0Pricing
Swift Academy · Lesson

Hashable and hash(into:)

Enable use as dictionary keys and set members.

What Is Hashable?

Hashable builds on Equatable and provides a hash value. It is required to use a type as a Set element or a Dictionary key.

Automatic Hashable

Like Equatable, structs and enums with Hashable members get it synthesized:

struct Point: Hashable {
    let x: Int
    let y: Int
}
let set: Set<Point> = [Point(x: 0, y: 0), Point(x: 0, y: 0)]
print(set.count)  // 1  -- duplicate collapsed

All lessons in this course

  1. Synthesized Equatable
  2. Hashable and hash(into:)
  3. Comparable and Sorting
  4. Custom Equality Semantics
← Back to Swift Academy