Difference between Swift's hash and hashValue

﹥>﹥吖頭↗ 提交于 2019-12-02 20:21:07
Martin R

hash is a required property in the NSObject protocol, which groups methods that are fundamental to all Objective-C objects, so that predates Swift. The default implementation just returns the objects address, as one can see in NSObject.mm, but one can override the property in NSObject subclasses.

hashValue is a required property of the Swift Hashable protocol.

Both are connected via a NSObject extension defined in the Swift standard library in ObjectiveC.swift:

extension NSObject : Equatable, Hashable {
  /// The hash value.
  ///
  /// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`
  ///
  /// - Note: the hash value is not guaranteed to be stable across
  ///   different invocations of the same program.  Do not persist the
  ///   hash value across program runs.
  open var hashValue: Int {
    return hash
  }
}

public func == (lhs: NSObject, rhs: NSObject) -> Bool {
  return lhs.isEqual(rhs)
}

(For the meaning of open var, see What is the 'open' keyword in Swift?.)

So NSObject (and all subclasses) conform to the Hashable protocol, and the default hashValue implementation return the hash property of the object.

A similar relationship exists between the isEqual method of the NSObject protocol, and the == operator from the Equatable protocol: NSObject (and all subclasses) conform to the Equatable protocol, and the default == implementation calls the isEqual: method on the operands.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!