类实例没有区别,请参见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" ///从for的实现中
==``ObjectIdentifier也可以明显看出,该
实现只比较指向对象存储的指针:
public static func == (x: ObjectIdentifier, y: ObjectIdentifier) -> Bool { return Bool(Builtin.cmp_eq_RawPointer(x._value, y._value)) }这正是该
===操作
确实还有:
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,因此如果要为您的类实现该协议,将非常有用:
extension MyClass: Hashable { var hashValue: Int { return ObjectIdentifier(self).hashValue }}还可以为未定义的元类型(例如
ObjectIdentifier(Float.self))创建对象标识符
===。



