0Pricing
Swift Academy · Lesson

Synthesized Equatable

Get == for free and customize when needed.

What Is Equatable?

Equatable is the protocol that gives a type the == operator. Conforming lets you compare two values for equality and use them with contains, firstIndex, and more.

Automatic Conformance

For a struct whose stored properties are all Equatable, Swift synthesizes == for you — just declare conformance:

struct Point: Equatable {
    let x: Int
    let y: Int
}
print(Point(x: 1, y: 2) == Point(x: 1, y: 2))  // true
print(Point(x: 1, y: 2) == Point(x: 9, y: 2))  // false

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