栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用ObjectIdentifier()和'==='运算符之间的区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用ObjectIdentifier()和'==='运算符之间的区别

类实例没有区别,请参见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)
)创建对象标识符
===



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393339.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号