Stage 1 · Lesson 5 of 10
Enums with associated values
are far more than a list of named constants. These are : each case can carry its own data, as in case loaded([Post]) and case failed(String). That makes them the natural way to model states such as loading, loaded or failed.
A switch over an enum must be exhaustive, meaning it handles every case. So adding a case makes the compiler show you every place to update. Enums can have methods and computed properties.
They can also have raw values, such as case small = "S", for simple mappings. Use if case let to match just one case.
Common trapadding a default case to a switch over your own enum: it silences the compiler when you later add a new case.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with
- CourseFree100 Days of SwiftUI
Paul Hudson (Hacking with Swift) · Beginner
Days 1 to 15 teach Swift, with a checkpoint exercise every other day