Powerful switch with pattern matching
Write expressive switches: match associated values, ranges, tuples, and add where-clauses. Keep branches exhaustive and readable.
Powerful switch with pattern matching is a free Swift Academy lesson on CoddyKit — lesson 2 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why pattern matching?
Swift switch matches patterns: enums with payloads, numeric ranges, tuples, and where conditions. It is also exhaustive.
Associated values
Bind associated values using let/var directly in the case pattern.
enum Network {
case success(code: Int, body: String)
case failure(message: String)
case offline
}
let r1: Network = .success(code: 200, body: "OK")
switch r1 {
case .success(let code, let body):
print("success", code, body)
case .failure(let msg):
print("fail:", msg)
case .offline:
print("offline")
}Range matching
Match numeric ranges like 90...100 or 80..<90. Use , to match multiple patterns in one case.
let score = 73
switch score {
case 90...100:
print("A")
case 80..<90:
print("B")
case 60..<80:
print("C")
case 0..<60:
print("D")
default:
print("invalid")
}Tuples & where
Match tuples and refine with a where condition for readable geometry-like checks.
let point = (x: -2, y: 3)
switch point {
case (0, 0):
print("origin")
case (let x, 0):
print("x-axis at", x)
case (0, let y):
print("y-axis at", y)
case (let x, let y) where x > 0 && y > 0:
print("Q1:", x, y)
default:
print("somewhere else")
}Wildcards & partial
Use _ to ignore fields, or bind only what you need. Keep branches small and clear.
enum Event {
case message(String, priority: Int)
case ping
}
let e: Event = .message("update", priority: 1)
switch e {
case .message(let text, priority: _):
print("msg:", text) // ignore priority with _
case .ping:
print("ping")
}Tips
Tips:
- Prefer specific cases first; keep default for the rare rest.
- Bind values with case let and refine using where.
- Design enums so switch can be exhaustive without a default when possible.
Switch power
Quick check: Why is Swift's switch more expressive?
Recap
Recap: Use switch to match patterns—bind enum payloads, target ranges, combine tuples, and filter with where—while staying exhaustive.
Frequently asked questions
Is the “Powerful switch with pattern matching” lesson free?
Yes — the full text of “Powerful switch with pattern matching” is free to read here on the web, and the Swift Academy course includes 3 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 “Powerful switch with pattern matching”?
Write expressive switches: match associated values, ranges, tuples, and add where-clauses. Keep branches exhaustive and readable. 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 3, so you can start here or from the beginning and move at your own pace.
How long does the “Powerful switch with pattern matching” 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
- Enums with Power basics
- Powerful switch with pattern matching
- Finite state modeling