用法一
==(_:_:)
nil 。聲明
static func == (lhs: Wrapped?, rhs: _OptionalNilComparisonType) -> Bool
參數
lhs要與
nil進行比較的值。rhsnil文字。
詳述
即使包裝值的類型不符合 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
參數
lhsnil文字。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 ==(_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
