使用ObjectIdentifier()和'==='运算符之间的区别 [英] Difference between using ObjectIdentifier() and '===' Operator

查看:561
本文介绍了使用ObjectIdentifier()和'==='运算符之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在Swift中实现一个根类,我声明它采用了Equatable协议(我想知道我类型的数组是否包含给定的实例).

Let's say I'm implementing a root class in Swift, which I declare adopts the Equatable protocol (I want to be able to tell if an array of my type contains a given instance or not).

在这种特定情况下,如果将协议的必需==运算符实现为以下方式,则有什么区别:

What is the difference -if any, in this specific case- between implementing the protocol's required == operator as:

public static func ==(lhs: MyClass, rhs: MyClass) -> Bool {

    return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}

...而不是仅仅这样做:

...as opposed to just doing this:

public static func ==(lhs: MyClass, rhs: MyClass) -> Bool {

    return (lhs === rhs)
}


作为参考,这是文档中关于ObjectIdentifier()的内容:


As a reference, this is what the documentation says about ObjectIdentifier():

类实例或元类型的唯一标识符.在Swift中,只有 类实例和元类型具有唯一的标识.没有 结构,枚举,函数或元组的身份概念.

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.

...这就是

...and this is what the "Basic Operators" section of The Swift Programming Language (Swift 3) says about the === operator:

注意

Swift还提供了两个标识运算符(===!==),您可以使用它们来测试两个对象引用是否都引用同一对象实例.有关更多信息,请参见类和结构.

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.

推荐答案

类实例没有区别,请参见以下内容 在ObjectIdentifier.swift中的注释:

There is no difference for class instance, see the following 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"
  ///

ObjectIdentifier实施== , 只是将指针与对象存储进行比较:

It also becomes apparent from the implementation of == for ObjectIdentifier, which just compares the pointers to the object storage:

  public static func == (x: ObjectIdentifier, y: ObjectIdentifier) -> Bool {
    return Bool(Builtin.cmp_eq_RawPointer(x._value, y._value))
  }

这是 ===运算符 效果还不错:

which is what the === operator does as well:

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,因此如果要为您的类实现该协议,将非常有用:

ObjectIdentifier conforms to Hashable, so it is useful if you want to implement that protocol for your class:

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

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

An object identifier can also be created for meta types (e.g. ObjectIdentifier(Float.self)) for which === is not defined.

这篇关于使用ObjectIdentifier()和'==='运算符之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆