Hashable
The Hashable
protocol is a fundamental protocol in Swift that allows types to be hashed into integer values, facilitating their use as keys in hash-based collections like dictionaries and sets.
In Swift, the following types conforms to the Hashable
protocol by default: Int
, UInt
, Float
, Double
, Bool
, String
, Character
, Tuples
, Optionals
, Enums
.
Syntax
protocol Hashable {
func hash(into myhasher: inout Hasher)
}
Conforming to the protocol automatically generates the hash()
method. This method accepts an inout Hasher
parameter (myhasher
), which is responsible for combining the hash values of the properties of the type.
Example Using a struct
In this example, the Person
struct
conforms to the Hashable
protocol by implementing hash()
. The hash value is calculated by combining the name
and age
properties using the myhasher.combine()
method. This allows instances of the Person
struct
to be stored in a set (personSet
) and efficiently looked up with their hashed values.
struct Person: Hashable {var id: Intvar name: Stringvar age: Intfunc hash(into myhasher: inout Hasher) {// Using id to uniquely identify each person.myhasher.combine(id)}}var personSet: Set<Person> = []let person1 = Person(id: 1, name: "Alice", age: 30)let person2 = Person(id: 2, name: "Bob", age: 25)personSet.insert(person1)personSet.insert(person2)print("Is Alice in the set? \(personSet.contains(person1) ? "Yes" : "No")")
This example results in the following output:
Is Alice in the set? Yes
Example Using an enum
In this example, an enum
named Color
contains three cases: red
, green
, and blue
. Since enum
cases are unique by definition, they are automatically hashable, and can be used in data structures like sets or dictionaries without any additional implementation of the Hashable
protocol.
enum Color: Hashable {case redcase greencase blue}let colorSet: Set<Color> = [.red, .green, .blue]for color in colorSet {print(color)}
This example results in the following output:
redgreenblue
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.