操作符
===(_:
===(_:_:)
返回一個布爾值,指示兩個引用是否指向同一個對象實例。
聲明
func === (lhs: AnyObject?, rhs: AnyObject?) -> Bool
參數
lhs
比較參考。
rhs
另一個比較參考。
詳述
此運算符測試兩個實例是否具有相同的標識,而不是相同的值。對於值相等,請參閱等於運算符 (==
) 和 Equatable
協議。
下麵的示例定義了一個IntegerRef
類型,一個具有引用語義的整數類型。
class IntegerRef: Equatable {
let value: Int
init(_ value: Int) {
self.value = value
}
}
func ==(lhs: IntegerRef, rhs: IntegerRef) -> Bool {
return lhs.value == rhs.value
}
因為IntegerRef
是一個類,所以可以使用identical-to 運算符(===
)比較它的實例。此外,由於IntegerRef
符合Equatable
協議,因此還可以使用等於運算符(==
)來比較實例。
let a = IntegerRef(10)
let b = a
print(a == b)
// Prints "true"
print(a === b)
// Prints "true"
identical-to 運算符 (===
) 在比較兩個對不同對象實例的引用時返回 false
,即使這兩個實例具有相同的值。
let c = IntegerRef(10)
print(a == c)
// Prints "true"
print(a === c)
// Prints "false"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift ==(_:_:)用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 ===(_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。