Working With Hash Values and the Hashable Protocol

Alex Mason
3 min readOct 10, 2016

--

Hashes and hash functions are prevalent features in many math and computer science concepts, and Swift allows you to work with them. A hash value allows the comparison of two values of the same type. String, Int, Double, and Bool are all hashable by default and conform to the Hashable protocol. The Hashable protocol in turn must conform to the Equatable protocol.

First let’s look at a quick example of hash values

In this example a is simply a constant of type String. Using the hashValue property, we can return an Int. To conform to the Hashable protocol, a type must have a getter property, hashValue, that will return an Int. More on that later.

If we make two strings we can compare the hash values of these two Strings.

Returns true

This will return true; both constants have the same hash value. However, the String “foo” will not always have the same hash value. In different environments and executions of your program, the hashValues may be of a different value, so do not save hashValues to use in later executions of your program. If you have two Strings of the same value, “bar” and “bar” for example, their hashValues will be equivalent in any environment. But, two values with the same hashValue are not necessarily equivalent.

What Is A Hash Value

What is a hash value? A hash value is derived from a hash function, which maps data of an arbitrary size to data of a fixed size. Hash values can represent large amounts of data as unique smaller values. In Swift, types that conform to the Hashable protocol map data to a value of type Int.

Hash values are often used to quickly retrieve or map data to a collection. When would you need to use a hash value? When comparing two values or when you’re trying to map to or read from two of Swift’s three generic collection types: dictionary, or sets. You can use any type that conforms to the ‘Hashable’ protocol in a set or as a dictionary key.

Making Your Custom Types Hashable

You can add Hashable conformance to your custom types by making a hashValue property. A hash value for your custom type will be an Int that is the same for all objects that compare equally. It must follow that if a == b then a.hashValue == b.hashValue. Because the Hashable protocol conforms to the Equatable protocol, which means implementations of the Hashable protocol must conform to a certain implementation for the (==) operator. So in your implementation: (a == a). If (a == b) then (b ==a). And finally, if (a == b) and (b == c) then (a == c).

Let’s make a custom class and conform it to the Hashable protocol.

We get an error for not conforming to 2 protocols

What we need to do is add a hashValue property to conform to the Hashable protocol.

Adding class property to return hashValue

But this is not enough! This clears the error for not conforming to the Hashable protocol but now we are not conforming to the Equatable protocol! We need to write a function that can compare to other instances of our class.

A static func is needed so it cannot be overriden

All done! Now that your class conforms to the Hashable protocol you can now use instances of you class as keys to collections.

--

--

Alex Mason

iOS developer. 3D is the future. Interested in tech, economics, and trends.