December 2015
Intermediate to advanced
400 pages
13h 3m
English
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.
Listing 16.6 Using a computed property (Town.swift)
...lazy var townSize: Size = {var townSize: Size { get { switch self.population { case 0...10000: return Size.Small case 10001...100000: return Size.Medium ...
Read now
Unlock full access