Computed Properties
You can use computed properties with any class, struct, or enum that you define. Computed properties do not store values like the properties that you have been working with thus far. Instead, a computed property provides a getter and optional setter to retrieve or set the property’s value. This difference allows the value of a computed property to change, unlike the value of a lazy stored property.
Replace your definition of the townSize property on the Town type with a computed read-only property.
Unlike stored read-only properties, computed read-only properties are defined using var.
Listing 16.6 Using a computed property (Town.swift)
...lazyvar townSize: Size={ get { switch self.population { case 0...10_000: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access