Swift Academy · Lesson

Optional Pattern in switch and for

Match optionals with case let patterns.

Lesson 4 of 413 steps

Optional Pattern in switch and for is a free Swift Academy lesson on CoddyKit. This is lesson 4 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.

Optionals in switch

You can switch directly on an optional. The cases .some(value) and .none match a present value or nil.

let value: Int? = 7
switch value {
case .some(let n):
    print("Got \(n)")
case .none:
    print("Nothing")
}

The ? Shorthand Pattern

Writing case let x? is shorthand for .some(x). The trailing ? means "unwrap into x".

let value: String? = "Swift"
switch value {
case let text?:
    print(text.uppercased())
case nil:
    print("none")
}

Matching nil Directly

You can match the nil case with the literal nil instead of .none, which reads more naturally.

let value: Int? = nil
switch value {
case let n?:
    print(n)
case nil:
    print("no value")
}

Matching Specific Values

You can mix specific unwrapped values with the general unwrap pattern.

let status: Int? = 404
switch status {
case 200?:
    print("OK")
case 404?:
    print("Not Found")
case let code?:
    print("Other: \(code)")
case nil:
    print("No status")
}

for case let in Loops

When iterating over optionals, for case let x? in array automatically skips nil elements and unwraps the rest.

let items: [Int?] = [1, nil, 3, nil, 5]
for case let n? in items {
    print(n)
}

How for case Filters

The pattern case let n? only matches non-nil elements, so the loop body runs solely for present values.

let names: [String?] = ["Ann", nil, "Bo"]
for case let name? in names {
    print(name.count)
}

Equivalent With if let

You could achieve the same with if let inside a plain loop, but for case let x? is more concise.

let items: [Int?] = [2, nil, 4]
for item in items {
    if let item {
        print(item)
    }
}

Matching Enum Optionals

The optional pattern composes with other patterns, like matching specific values inside the optional.

let codes: [Int?] = [1, 2, nil, 3]
for case let c? in codes where c % 2 == 1 {
    print("odd: \(c)")
}

where With for case

Add a where clause to filter further after unwrapping in a for case loop.

let scores: [Int?] = [40, nil, 80, 95]
for case let s? in scores where s >= 80 {
    print("high: \(s)")
}

Switch With where

Inside a switch on an optional, the unwrapped binding can be refined with a where clause.

let value: Int? = 12
switch value {
case let n? where n > 10:
    print("big: \(n)")
case let n?:
    print("small: \(n)")
case nil:
    print("none")
}

Why Use These Patterns

Optional patterns let you handle present and absent values in a single, exhaustive structure, reducing scattered nil checks.

let result: String? = nil
switch result {
case let r?:
    print("Value: \(r)")
case nil:
    print("Empty result")
}

Quick Check

Test optional patterns.

Recap: Optional Patterns in switch and for

Use case let x? (shorthand for .some) and case nil in a switch, and for case let x? to iterate non-nil elements while unwrapping them, optionally refined with where.

let xs: [Int?] = [10, nil, 20]
for case let x? in xs { print(x) }
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 “Optional Pattern in switch and for” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Optional Pattern in switch and for”, 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 “Optional Pattern in switch and for”?

Match optionals with case let patterns. 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 4 of 4.

How long does the “Optional Pattern in switch and for” 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. if let and Shorthand Binding
  2. guard let for Early Exit
  3. Binding Multiple Optionals
  4. Optional Pattern in switch and for
← Back to Swift Academy