Hashable in Swift is a protocol, in which objects can conform to. It is used to compare two instances. Classes require implementing Equatable protocol prior to Hashable. Hashable already conforms to Equatable protocol.
A hashValue is a long integer that represents a not-guaranteed-but-hopefully-most-of-the-times unique number that represents the contents of that type.
In a case of a Dictionary, a hashValue is used to determine where in memory a key’s value is stored, so the access in a time complexity is a constant O(1).
For detailed articles on Hashable and hasher in Swift, see: https://swiftrocks.com/how-hashable-works-in-swift
Comments
Post a Comment