Swift Academy · Lesson

Associated Values for Rich Data

Attaching payloads to cases and extracting them with pattern matching.

Lesson 2 of 412 steps

Associated Values for Rich Data is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

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

Yes — any 3 lessons of the Swift Academy learning path, including “Associated Values for Rich Data”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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