Swift Easy · Lesson 1 · On the house

Enums and switch

Cases, associated values, exhaustive switch. Lesson 1 is free — on the house.

Lessons · Swift Easy · Lesson 1 of 10 · On the house

Enums and switch

Cases, associated values, exhaustive switch.

Illustration for the Swift course
Lesson 1 is on the house. The rest opens with the paper, the Daily, or a gift.

This is Swift · easy, lesson 1: Enums and switch. Read the teaching and the stills before the checks. The quizzes are open book and test the subject — Swift itself — not product rules or layout trivia.

You already finished Introductory. I will not re-teach the absolute first hello. We build upward.

An enum names a closed set of cases. Simple cases look like case north, south. Associated values attach payload to a case: case tip(Int) carries an Int when that case is chosen.

switch on an enum must be exhaustive: every case is handled, or you include default. The compiler rejects incomplete switches — that is a feature, not noise.

Bind associated values inside a case: case .tip(let amount):. Pattern matching unpacks the payload and keeps the rest of the branch typed.

Enums are value types. Prefer them when a value is one of a known set of shapes rather than an open-ended class hierarchy.

Misses: forgetting a case (non-exhaustive switch), treating associated values like stored properties on the enum type itself, or writing case tip = 1 raw-value style when you meant an associated Int.

Put Enums and switch in a sentence you could teach a friend: what changes, what stays the same, and what you expect from the still.

Change one dial at a time when you experiment, then re-run. Batch changes invent ghost bugs.

Predict the still on paper before you type. Prediction turns running into confirmation.

Name each token in the still aloud. Hesitation marks the exact gap.

When something fails, read the last error line first. Calm retries beat rewriting everything.

Open-book means the page is a tool. Pick the claim that matches what we traced.

Keep mistaken traces beside corrected ones. The diff is the real lesson.

enum-switch

enum Fare {
    case adult
    case child
    case tip(Int)
}
func label(_ f: Fare) -> String {
    switch f {
    case .adult: return "adult"
    case .child: return "child"
    case .tip(let n): return "tip \(n)"
    }
}
print(label(.tip(5)))
Match this when you type. Open book.

Sit with that still. Trace it top to bottom. Name each piece. The exercise asks you to match its essence.

If something fails when you try it yourself, change one thing. Read the error. Return to the still. I will not rush you.

Quiz

What does an enum define?

Quiz

Must switch on an enum be exhaustive?

Quiz

How do you bind an associated value?

Quiz

Are enums value types?

Quiz

What does case tip(Int) mean?

Quiz

Incomplete switch on enum?

Quiz

Based on today's teaching, which claim is right?

Quiz

Based on today's teaching, which claim is right?

Check

Match the still for: Enums and switch.

That is the lesson. When every quiz and exercise on this page is green, mark it passed. Next lesson when you are ready.

Open-book: the answers are on this page. Pass every quiz and check (9) to mark the lesson done. This visit: 0/9.

Enums and switch · Swift Easy (free) — The Gold Standard