假设我正在Swift中实现一个根类,该类声明采用Equatable
协议(protocol)(我希望能够知道我类型的数组是否包含给定的实例)。
在这种特定情况下,-(如果有)在实现协议(protocol)所需的==
运算符之间有什么区别:
public static func ==(lhs: MyClass, rhs: MyClass) -> Bool {
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}
...而不是仅仅这样做:
public static func ==(lhs: MyClass, rhs: MyClass) -> Bool {
return (lhs === rhs)
}
作为引用,这是文档中有关
ObjectIdentifier()
的内容:...这就是The Swift Programming Language (Swift 3)的“基本运算符”部分对
===
运算符的描述:最佳答案
类实例没有区别,请参见以下内容
comments in ObjectIdentifier.swift:
/// Creates an instance that uniquely identifies the given class instance.
///
/// The following example creates an example class `A` and compares instances
/// of the class using their object identifiers and the identical-to
/// operator (`===`):
///
/// class IntegerRef {
/// let value: Int
/// init(_ value: Int) {
/// self.value = value
/// }
/// }
///
/// let x = IntegerRef(10)
/// let y = x
///
/// print(ObjectIdentifier(x) == ObjectIdentifier(y))
/// // Prints "true"
/// print(x === y)
/// // Prints "true"
///
/// let z = IntegerRef(10)
/// print(ObjectIdentifier(x) == ObjectIdentifier(z))
/// // Prints "false"
/// print(x === z)
/// // Prints "false"
///
从
implementation of
==
for ObjectIdentifier
,只是将指针与对象存储进行比较:
public static func == (x: ObjectIdentifier, y: ObjectIdentifier) -> Bool {
return Bool(Builtin.cmp_eq_RawPointer(x._value, y._value))
}
这是the
===
operator也可以:
public func === (lhs: AnyObject?, rhs: AnyObject?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return Bool(Builtin.cmp_eq_RawPointer(
Builtin.bridgeToRawPointer(Builtin.castToUnknownObject(l)),
Builtin.bridgeToRawPointer(Builtin.castToUnknownObject(r))
))
case (nil, nil):
return true
default:
return false
}
}
ObjectIdentifier
符合Hashable
,因此如果要为您的类实现该协议(protocol),它将非常有用:extension MyClass: Hashable {
var hashValue: Int {
return ObjectIdentifier(self).hashValue
}
}
还可以为元类型创建对象标识符
(例如
ObjectIdentifier(Float.self)
)未为其定义===
的代码。