0Pricing
Swift Academy · Lesson

Associated Values for Rich Data

Attaching payloads to cases and extracting them with pattern matching.

Associated Values for Rich Data is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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") ```

Extracting with switch

```swift switch barcode { case .upc(let n, let m, let p, let c): print("UPC: \(n)-\(m)-\(p)-\(c)") case .qrCode(let code): print("QR: \(code)") } ``` Or use `case let` to bind all at once: ```swift case let .upc(n,m,p,c): ... ```

Named Associated Values

```swift enum NetworkResult { case success(statusCode: Int, data: Data) case failure(error: Error) } if case .success(let code, let data) = result { ... } ``` Labels make the binding context clear.

Associated Values vs Raw Values

• Raw values: compile-time constants, all cases same type, `rawValue` property • Associated values: runtime data, each case its own type, extracted via pattern matching You cannot have both in the same enum.

Optionals as Enums

Swift's `Optional` is literally: ```swift enum Optional { case some(Wrapped) case none } ``` Every `if let`, `switch`, `??` operates on this same mechanism.

Result<Success, Failure>

```swift func fetch() -> Result { ... } switch fetch() { case .success(let data): process(data) case .failure(let err): print(err) } ``` `Result` is a standard library enum with two associated-value cases.

Pattern Matching in if case

```swift let item = Barcode.qrCode("hello") if case .qrCode(let code) = item { print("QR code: \(code)") } ``` `if case` is concise when you only care about one case.

Guard Case

```swift func process(_ result: NetworkResult) { guard case .success(_, let data) = result else { return } handle(data) } ``` `guard case` exits early if the pattern doesn't match.

Enums in Collections

```swift let events: [AppEvent] = [.login("alice"), .pageView("/home"), .logout] let logins = events.compactMap { if case .login(let u) = $0 { return u } else { return nil } } ```

Quick Check

What keyword is used to extract associated values inside a switch case?

Recap

Key takeaways: • Associated values attach data to individual cases • Extract with `switch`, `if case`, or `guard case` • Use named labels for clarity: `.success(code: Int, data: Data)` • `Optional` and `Result` are standard library enums with associated values • Cannot mix associated values and raw values in one enum Next: indirect enums and recursive structures.

Frequently asked questions

Is the “Associated Values for Rich Data” lesson free?

Yes — the full text of “Associated Values for Rich Data” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Associated Values for Rich Data”?

Attaching payloads to cases and extracting them with pattern matching. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Associated Values for Rich Data” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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