0Pricing
Swift Academy · Lesson

Modeling State Machines with Enums

Representing app state transitions safely with Swift enums.

Welcome

Enums are ideal for modelling finite state machines. Each case is a state; transitions are functions that return the next state. This approach makes illegal states unrepresentable.

Simple State Enum

```swift enum TrafficLight { case red, yellow, green func next() -> TrafficLight { switch self { case .red: return .green case .green: return .yellow case .yellow: return .red } } } var light = TrafficLight.red light = light.next() // .green ```

All lessons in this course

  1. Raw Values and CaseIterable
  2. Associated Values for Rich Data
  3. Indirect Enums and Recursive Structures
  4. Modeling State Machines with Enums
← Back to Swift Academy