🎓 A1Learner
🔍
0 of 36 lessons completed

Swift Standard Protocols

Standard protocols are protocols supplied with the standard library. A type conforms to one to gain behaviour such as comparing, hashing, sorting, or describing its values.

Synthesized equality

The Point struct conforms to Equatable and writes no == of its own. The compiler synthesizes == from the two stored properties, so two equal points compare as true.

struct Point: Equatable {
    var x: Int
    var y: Int
}
let first = Point(x: 3, y: 4)
let second = Point(x: 3, y: 4)
let third = Point(x: 5, y: 4)
print(first == second)
print(first == third)

Hashable values inside a set

The Tag struct conforms to Hashable, so tags can live in a set. The name swift is added twice yet stored once, and the names print in sorted order.

struct Tag: Hashable {
    var name: String
}
let tags: Set<Tag> = [Tag(name: "swift"), Tag(name: "code"), Tag(name: "swift"), Tag(name: "data")]
print(tags.count)
var names: [String] = []
for tag in tags {
    names.append(tag.name)
}
for name in names.sorted() {
    print(name)
}

A custom order with Comparable

The Score struct conforms to Comparable and supplies a < function based on points. Sorting the scores places the smallest points first.

struct Score: Comparable {
    var points: Int
    var name: String
    static func < (lhs: Score, rhs: Score) -> Bool {
        return lhs.points < rhs.points
    }
}
let scores = [Score(points: 30, name: "Ana"), Score(points: 10, name: "Bo"), Score(points: 20, name: "Cy")]
let ordered = scores.sorted()
for score in ordered {
    print(score.name)
}

A description controls the print

The Book struct conforms to CustomStringConvertible and gives a description property. Printing the book prints the description text instead of the stored values.

struct Book: CustomStringConvertible {
    var title: String
    var description: String {
        return "Book titled \(title)"
    }
}
let book = Book(title: "Field Notes")
print(book)

Example

struct Coin: Equatable, CustomStringConvertible {
    var value: Int
    var description: String {
        return "\(value) coins"
    }
}
let first = Coin(value: 5)
let second = Coin(value: 5)
print(first)
print(first == second)

Key points

  • Equatable conformance lets two values be compared with the == operator.
  • A struct can receive a synthesized == when every stored property supports comparison.
  • Hashable conformance lets a value be placed in a set, where equal values are stored only once.
  • Comparable conformance needs a < function that reports whether the left value comes before the right value.
  • CustomStringConvertible conformance needs a description property, and printing the value shows that text.

In this example, the Coin struct conforms to Equatable and CustomStringConvertible with a value of 5. Printing the coin shows 5 coins because the description property controls the printed text. The comparison prints true because the synthesized == finds equal values.

Note

Set contents are printed in sorted order in this lesson, because a set itself keeps no order.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which protocol uses a description property to control how a value prints?