April 2026
Intermediate
683 pages
25h 29m
English
Computed properties in Swift enumerations allow you to derive and return values dynamically. Those computed properties are typically calculated based on the selected case.
Listing 7.18 demonstrates a simple example via the Coffee enumeration. There are three different cases here—espresso, americano, and coldBrew—which could correspond to coffee options in a small shop.
enum Coffee { case espresso case americano case coldBrew var slogan: String { switch self { case .espresso: return "Espresso, small but mighty" case .americano: return "Americano, the classic choice" case .coldBrew: return "Cold brew, what a breeze" } }}
Listing 7.18 Enumeration with Computed Property
In this example, there is a computed property ...
Read now
Unlock full access