Optional Pattern in switch and for
Match optionals with case let patterns.
Optional Pattern in switch and for is a free Swift Academy lesson on CoddyKit — lesson 4 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.
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) }Frequently asked questions
Is the “Optional Pattern in switch and for” lesson free?
Yes — the full text of “Optional Pattern in switch and for” 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 “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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- if let and Shorthand Binding
- guard let for Early Exit
- Binding Multiple Optionals
- Optional Pattern in switch and for