Pritesh
Pritesh

Reputation: 172

What is the use of hashable protocol in swift4?

please explain the use of hashable protocol with implementation in swift. Apple defines hashable as “a type that provides an integer a hash value.” Okay, but what’s a hash value?

Upvotes: 1

Views: 3860

Answers (4)

Benpaper
Benpaper

Reputation: 144

Quick answer:

We use hash integer into object to be able to quickly identify object that are equals by getting the object instance in front of the index that we are looking for.

Not quick answer:

When you are dealing with list in order to find an object you need to iterate over all your array and compare property to find the one you are looking for, this can slowdown your app as the list get bigger.

When you use SET, the mechanism under the hood use hash indexes to find an object so it take you only the time to calculate once the index you are looking for then you can access straight forward to your object, THIS IS SO COOL ISN'T. In order to use SET the object need to conform to Hashable protocol since Swift 4.1, if your class or struct and all properties conform to Hashable then the conformity with Hashable and Equatable protocol is automatically done for you under the hood. If you do not meet those requirments then you will have to make sure that you conform to Equatable and Hashable protocol.

Equatable protocol need to overide static func ==(..) in order to compare you object.

Hashable protocol need to provide as far as you can a unique integer value hashValue which need to be the same within two object when they are equals. Hope this help

Upvotes: 3

Learner
Learner

Reputation: 1137

To make an object conform to Hashable we need to provide a hashValue property that will return a unique, consistent number for each instance. The Hashable protocol inherits from Equatable, so you may also need to implement an == function.

Note: If two objects compare as equal using == they should also generate the same hash value, but the reverse isn’t true – hash collisions can happen.

Before Swift 4.1, conforming to Hashable was complex because you needed to calculate a hashValue property by hand. In Swift 4.1 this improved so that hashValue could be synthesized on your behalf if all the properties conform to Hashable . Swift 4.2 introduces a new Hasher struct that provides a randomly seeded, universal hash function to make all our lives easier. Refer for more

Upvotes: 5

David Rönnqvist
David Rönnqvist

Reputation: 56625

The Hashable documentation give one concrete example of what it's for:

You can use any type that conforms to the Hashable protocol in a set or as a dictionary key.

You can think of a hash value as a quick approximation of equality. Two elements that are equal will have the same hash value but two elements with the same hash value are not guaranteed to actually be equal.

Upvotes: 0

J. Doe
J. Doe

Reputation: 13043

If an object conforms to the hashable protocol, it needs to have a hashValue, as you mentioned. The hashValue can be used to compare objects / uniquely identify the object.

You can compare objects in two ways:

  1. === function. This checks object references (can only be used with classes). It checks if the left object has the same reference to the right object. Even if both objects have exactly the same property values BUT they do have a different reference, it returns false.

  2. == function (Equatable protocol). It checks if the objects are equal to eachother based on the static func ==. You can return the hashValue of the object. In that way, you can say objects are equal to eachtoher based on the properties, rather than reference.

If you provide your own hashValue, you can say objects are equal to eachother in a way you say objects are equal to eachother, regardless of the reference to the object. You can use objects in a Set that conform to the hashable protocol, because a Set checks if objects are equal to eachother based on the hashValue.

Upvotes: 2

Related Questions