用法一
==(_:_:)
nil
。声明
static func == (lhs: Wrapped?, rhs: _OptionalNilComparisonType) -> Bool
参数
lhs
要与
nil
进行比较的值。rhs
nil
文字。
详述
即使包装值的类型不符合 Equatable
协议,您也可以使用此等于运算符 (==
) 来测试可选实例是否为 nil
。
以下示例将 stream
变量声明为假设的 DataStream
类型的可选实例。虽然 DataStream
不是 Equatable
类型,但此运算符允许检查 stream
是否为 nil
。
var stream: DataStream? = nil
if stream == nil {
print("No data stream is configured.")
}
// Prints "No data stream is configured."
可用版本
用法二
==(_:_:)
nil
。声明
static func == (lhs: _OptionalNilComparisonType, rhs: Wrapped?) -> Bool
参数
lhs
nil
文字。rhs
要与
nil
进行比较的值。
详述
即使包装值的类型不符合 Equatable
协议,您也可以使用此等于运算符 (==
) 来测试可选实例是否为 nil
。
以下示例将 stream
变量声明为假设的 DataStream
类型的可选实例。虽然 DataStream
不是 Equatable
类型,但此运算符允许检查 stream
是否为 nil
。
var stream: DataStream? = nil
if nil == stream {
print("No data stream is configured.")
}
// Prints "No data stream is configured."
可用版本
用法三
==(_:_:)
声明
static func == (lhs: Wrapped?, rhs: Wrapped?) -> Bool
Wrapped
符合 Equatable
时可用。参数
lhs
要比较的可选值。
rhs
要比较的另一个可选值。
详述
使用此等于运算符 (==
) 比较符合 Equatable
协议的类型的任意两个可选实例。如果两个参数都是 nil
或者如果两个参数包含相等的值,则比较返回 true
。相反,如果只有一个参数是nil
,或者如果两个参数包含不相等的值,则比较返回false
。
let group1 = [1, 2, 3, 4, 5]
let group2 = [1, 3, 5, 7, 9]
if group1.first == group2.first {
print("The two groups start the same.")
}
// Prints "The two groups start the same."
您还可以使用此运算符将非可选值与包装相同类型的可选值进行比较。在进行比较之前,非可选值被包装为可选值。在以下示例中,numberToMatch
常量在与可选的 numberFromString
进行比较之前被包装为可选的:
let numberToFind: Int = 23
let numberFromString: Int? = Int("23") // Optional(23)
if numberToFind == numberFromString {
print("It's a match!")
}
// Prints "It's a match!"
表示为文字的实例也可以与此运算符一起使用。在下一个示例中,将整数文字与可选整数 numberFromString
进行比较。文字 23
被推断为 Int
实例,然后在执行比较之前包装为可选项。
if 23 == numberFromString {
print("It's a match!")
}
// Prints "It's a match!"
可用版本
相关用法
- Swift Optional symbolVariant(_:)用法及代码示例
- Swift Optional popover(isPresented:attachmentAnchor:arrowEdge:content:)用法及代码示例
- Swift Optional mask(alignment:_:)用法及代码示例
- Swift Optional listSectionSeparatorTint(_:edges:)用法及代码示例
- Swift Optional badge(_:)用法及代码示例
- Swift Optional fullScreenCover(isPresented:onDismiss:content:)用法及代码示例
- Swift Optional keyboardType(_:)用法及代码示例
- Swift Optional clipShape(_:style:)用法及代码示例
- Swift Optional preferredColorScheme(_:)用法及代码示例
- Swift Optional background(_:ignoresSafeAreaEdges:)用法及代码示例
- Swift Optional saturation(_:)用法及代码示例
- Swift Optional focusSection()用法及代码示例
- Swift Optional overlay(alignment:content:)用法及代码示例
- Swift Optional colorMultiply(_:)用法及代码示例
- Swift Optional confirmationDialog(_:isPresented:titleVisibility:presenting:actions:message:)用法及代码示例
- Swift Optional offset(_:)用法及代码示例
- Swift Optional focused(_:equals:)用法及代码示例
- Swift Optional hidden()用法及代码示例
- Swift Optional previewDevice(_:)用法及代码示例
- Swift Optional keyboardShortcut(_:modifiers:localization:)用法及代码示例
- Swift Optional imageScale(_:)用法及代码示例
- Swift Optional accessibilityAction(named:_:)用法及代码示例
- Swift Optional textSelection(_:)用法及代码示例
- Swift Optional pageCommand(value:in:step:)用法及代码示例
- Swift Optional padding(_:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Optional ==(_:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。