Hashable

When you learned about the Dictionary and Set types in Chapter 10, Dictionaries and Chapter 11, Sets, you learned that in order for a type to be included in a Set or used as the key type in a Dictionary, it must be hashable. A type is hashable when it conforms to the Hashable protocol. Hashability has a straightforward purpose: the ability of a type to generate an integer based on its content. But why is that useful? You will explore hashing in this section.

Put some Point instances to work in a set and a dictionary to see the error that you get:

Example 25.12. Verifying Hashable conformance
...
let pointRange = c..<d                                     {{x 2, y 6, nil},...
pointRange.contains(a)                                     true
pointRange.contains(Point(x: -1, y: -1))                   false

let points: Set = ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.