加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 使用ObjectIdentifier()和’===’运算符之间的区别

发布时间:2020-12-14 05:47:54 所属栏目:百科 来源:网络整理
导读:假设我在Swift中实现了一个根类,我声明它采用了Equatable协议(我希望能够判断我的类型的数组是否包含给定的实例). 有什么区别 – 在这种特定情况下 – 在实现协议的required ==运算符之间有什么区别: public static func ==(lhs: MyClass,rhs: MyClass) - B
假设我在Swift中实现了一个根类,我声明它采用了Equatable协议(我希望能够判断我的类型的数组是否包含给定的实例).

有什么区别 – 在这种特定情况下 – 在实现协议的required ==运算符之间有什么区别:

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()的内容:

A unique identifier for a class instance or metatype. In Swift,only
class instances and metatypes have unique identities. There is no
notion of identity for structs,enums,functions,or tuples.

…这就是The Swift Programming Language (Swift 3)的“基本运算符”部分对===运算符的说法:

NOTE

Swift also provides two identity operators (=== and !==),which you use to test whether two object references both refer to the same object instance. For more information,see Classes and Structures.

类实例没有区别,请参阅以下内容
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,因此如果您想为您的类实现该协议,它会很有用:

extension MyClass: Hashable {
    var hashValue: Int {
        return ObjectIdentifier(self).hashValue
    }
}

还可以为元类型创建对象标识符(例如,ObjectIdentifier(Float.self)),其中未定义===.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读