Enums in Swift: Cases, Raw Values, Associated Values, Pattern Matching and CaseIterable

Swift 60 min min read Updated: Nov 21, 2026 Intermediate
Enums in Swift: Cases, Raw Values, Associated Values, Pattern Matching and CaseIterable
Intermediate Topic 3 of 3

Enums Name a Fixed Set of Options

swift enum Direction { case north, south, east, west } let d = Direction.east switch d { case .north, .south: print("vertical") case .east, .west: print("horizontal") }
Output

horizontal

Raw Values and CaseIterable

swift enum Planet: Int, CaseIterable { case mercury = 1, venus, earth } print(Planet.earth.rawValue, Planet(rawValue: 2) == .venus, Planet(rawValue: 9) == nil) print(Planet.allCases.count) enum Level: String { case low, high } print(Level.high.rawValue)
Output

3 true true | 3 | high

Associated Values

Each case can carry its own data. This is one of Swift's best features for modeling states.

swift enum Payment { case cash(amount: Double) case card(number: String, amount: Double) case upi(id: String) } func describe(_ p: Payment) -> String { switch p { case .cash(let amount): return "cash \(amount)" case .card(let number, let amount): return "card ending \(number.suffix(4)) for \(amount)" case .upi(let id): return "upi to \(id)" } } print(describe(.cash(amount: 50))) print(describe(.card(number: "1234567890123456", amount: 75.5))) print(describe(.upi(id: "shop@bank")))
Output

cash 50.0 | card ending 3456 for 75.5 | upi to shop@bank

Methods and Computed Properties

swift enum Light { case red, green, yellow var next: Light { switch self { case .red: return .green case .green: return .yellow case .yellow: return .red } } } var l = Light.red for _ in 1...4 { print(l, terminator: " "); l = l.next } print("")
Output

red green yellow red

Modeling Loading States

swift enum LoadState { case idle case loading case loaded([String]) case failed(message: String) } func render(_ s: LoadState) -> String { switch s { case .idle: return "Tap to load" case .loading: return "Loading..." case .loaded(let items): return "\(items.count) items" case .failed(let message): return "Error: \(message)" } } print(render(.idle), "|", render(.loaded(["a", "b"])), "|", render(.failed(message: "offline")))
Output

Tap to load | 2 items | Error: offline

Why this is powerful With an enum, impossible states cannot exist. You cannot be loading and failed at the same time. The compiler also forces your switch to cover every case.

Common Mistakes

  • Using strings or numbers where an enum would prevent typos
  • Adding default to a switch and hiding new cases
  • Using many Bool flags instead of one enum for state

Practice

  1. Model a Shape enum with associated sizes and compute area
  2. Write a TrafficLight enum with a duration property
  3. Model a Result-like enum of your own

Conclusion

Enums with associated values describe exactly the situations your program can be in.

Quick Summary: Cases; raw values; associated values; state modeling.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators