The Hashable
protocol in Swift requires you to implement a property called hashValue
:
protocol Hashable : Equatable { /// Returns the hash value. 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. /// /// The value of `hashValue` property must be consistent with the equality /// comparison: if two values compare equal, they must have equal hash /// values. var hashValue: Int { get } }
However, it seems there's also a similar property called hash
.
What is the difference between hash
and hashValue
?
A hash value is an integer representation of some other value. In Swift that's a 64-bit integer, which gives quite a bit of room for all your things: just over eighteen quintillion, or 18,446,744,073,709,551,615.
In Swift, a Hashable is a protocol that provides a hashValue to our object. The hashValue is used to compare two instances. To use the hashValue , we first have to conform (associate) the type (struct, class, etc) to Hashable property.
In Swift, the Hashable protocol is the main component used to provide hashing capabilities to custom types. It is most commonly used to allow types to be used as the key of Dictionaries and Sets , but it also gives you access to `Sequence` helper methods like `contains(_:)`.
A hash value is a numeric value of a fixed length that uniquely identifies data. Hash values represent large amounts of data as much smaller numeric values, so they are used with digital signatures.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With