操作符
~=(_:
~=(_:_:)
返回一個布爾值,指示兩個參數是否按值相等匹配。
聲明
func ~= <T>(a: T, b: T) -> Bool where T : Equatable
參數
lhs
要比較的值。
rhs
另一個要比較的值。
詳述
pattern-matching 運算符 (~=
) 在 case
語句內部用於模式匹配。當您匹配 case
語句中的 Equatable
值時,會在後台調用此運算符。
let weekday = 3
let lunch: String
switch weekday {
case 3:
lunch = "Taco Tuesday!"
default:
lunch = "Pizza again."
}
// lunch == "Taco Tuesday!"
在此示例中,case 3
表達式使用此 pattern-matching 運算符來測試 weekday
是否等於值 3
。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift KeyValuePairs flatMap(_:)用法及代碼示例
- Swift String.UTF8View first用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Int8 ~(_:)用法及代碼示例
- Swift SetAlgebra isStrictSubset(of:)用法及代碼示例
- Swift UInt +(_:)用法及代碼示例
- Swift Array enumerated()用法及代碼示例
- Swift FlattenSequence prefix(_:)用法及代碼示例
- Swift Slice endIndex用法及代碼示例
- Swift LazySequence split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift MutableCollection partition(by:)用法及代碼示例
- Swift ReversedCollection min(by:)用法及代碼示例
- Swift RandomNumberGenerator用法及代碼示例
- Swift Dictionary.Keys shuffled()用法及代碼示例
- Swift AnySequence elementsEqual(_:)用法及代碼示例
- Swift UInt &<<(_:_:)用法及代碼示例
- Swift Optional.Publisher tryDrop(while:)用法及代碼示例
- Swift DefaultIndices endIndex用法及代碼示例
- Swift Substring.UnicodeScalarView insert(contentsOf:at:)用法及代碼示例
- Swift LazyFilterSequence dropFirst(_:)用法及代碼示例
- Swift LazySequence suffix(from:)用法及代碼示例
- Swift ArraySlice starts(with:)用法及代碼示例
- Swift Int16.Words max()用法及代碼示例
- Swift ArraySlice reduce(_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 ~=(_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。