0Pricing
Swift Academy · Lesson

Associated Values for Rich Data

Attaching payloads to cases and extracting them with pattern matching.

Welcome

Associated values let each enum case carry additional data. They turn enums into powerful discriminated unions — the foundation of type-safe state modelling in Swift.

Defining Associated Values

```swift enum Barcode { case upc(Int, Int, Int, Int) case qrCode(String) } let product = Barcode.upc(8,85909,51226,3) let web = Barcode.qrCode("https://example.com") ```

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