Swift Switch
A switch compares one value against a list of patterns and runs the code of the first one that matches. Swift's switch is much more powerful than the version in C or Java: it can match ranges, tuples, several values at once and extra conditions — and the compiler checks that you have covered every possible case.
Basic switch
let day = "Sat"
switch day {
case "Mon", "Tue", "Wed", "Thu", "Fri":
print("Work day")
case "Sat", "Sun":
print("Weekend!")
default:
print("That's not a day")
}Weekend!
Notice three things that differ from C-style languages:
| Rule | What it means |
|---|---|
No break needed |
A case ends automatically; execution never "falls through" into the next case |
| Several values per case | Separate them with commas: case "Sat", "Sun": |
| Must be exhaustive | Every possible value must be handled — usually with default |
Exhaustiveness
A String or Int has endless possible values, so you need a default case. If you forget it, the compiler stops you:
let level = 2
switch level {
case 1:
print("Easy")
case 2:
print("Medium")
}error: emit-module command failed with exit code 1 (use -v to see invocation)
<source>:3:1: error: switch must be exhaustive
1 | let level = 2
2 |
3 | switch level {
| |- error: switch must be exhaustive
| `- note: add a default clause
4 | case 1:
5 | print("Easy")
When you switch over an enum or a Bool, listing every case is enough — no default is required. You will see this in the Enums chapter.
Matching ranges
A case can be a range. This makes grading, pricing tiers and similar logic very readable:
let speeds = [0, 25, 60, 130]
for speed in speeds {
switch speed {
case 0:
print("\(speed) km/h: standing still")
case 1..<30:
print("\(speed) km/h: slow")
case 30...100:
print("\(speed) km/h: normal")
default:
print("\(speed) km/h: too fast!")
}
}0 km/h: standing still
25 km/h: slow
60 km/h: normal
130 km/h: too fast!
Tuples, value binding and where
You can switch over a tuple and match each element separately. _ means "anything", let binds a value to a name you can use inside the case, and where adds an extra condition:
let points = [(0, 0), (3, 0), (0, -2), (4, 4), (2, 7)]
for point in points {
switch point {
case (0, 0):
print("\(point) is the origin")
case (_, 0):
print("\(point) lies on the x-axis")
case (0, let y):
print("\(point) lies on the y-axis at y = \(y)")
case let (x, y) where x == y:
print("\(point) is on the diagonal")
case let (x, y):
print("\(point) is somewhere else (x: \(x), y: \(y))")
}
}(0, 0) is the origin
(3, 0) lies on the x-axis
(0, -2) lies on the y-axis at y = -2
(4, 4) is on the diagonal
(2, 7) is somewhere else (x: 2, y: 7)
The last case case let (x, y) matches every remaining tuple, so this switch is exhaustive without a default.
fallthrough
If you really want C-style behaviour, fallthrough continues into the next case's body (without checking its pattern). It is rarely needed:
let stars = 4
var perks: [String] = []
switch stars {
case 5:
perks.append("free breakfast")
fallthrough
case 4:
perks.append("pool access")
fallthrough
case 3:
perks.append("wifi")
default:
break
}
print("\(stars)-star hotel perks: \(perks)")4-star hotel perks: ["pool access", "wifi"]
Note: A case body can't be empty. Write
breakwhen a case should simply do nothing, like thedefaultabove.
switch as an expression
Like if, a switch can return a value directly (Swift 5.9+), as long as every case is one expression:
let code = 404
let message = switch code {
case 200: "OK"
case 301, 302: "Redirect"
case 400..<500: "Client error"
case 500..<600: "Server error"
default: "Unknown"
}
print("\(code): \(message)")404: Client error
Test yourself
Why don't Swift switch cases need break?
- Swift has no break keyword
- Cases never fall through; each one ends automatically
- The compiler inserts goto statements
- Only the default case needs it
A switch over an Int without a default case and with only case 1: and case 2:…
- compiles and ignores other values
- fails to compile: switch must be exhaustive
- crashes for other values
- runs case 1 for other values
Which case matches the tuple (5, 5) first?
case (_, 0) / case let (x, y) where x == y / case let (x, y)
- case (_, 0)
- case let (x, y) where x == y
- case let (x, y)
- None of them