Swift requires switch statements to cover all possible cases. This helps avoid mistakes by making sure you handle every option.
Switch must be exhaustive in Swift
switch value { case pattern1: // code case pattern2: // code // ... all cases must be covered }
All possible cases must be covered, either explicitly or with a default case.
If you use an enum, the switch must handle every enum case or include default.
enum Direction { case north, south, east, west } let dir = Direction.north switch dir { case .north: print("Going north") case .south: print("Going south") case .east: print("Going east") case .west: print("Going west") }
default covers all other values, making the switch exhaustive.let number = 3 switch number { case 1: print("One") case 2: print("Two") default: print("Other number") }
This program uses a switch on an enum with all cases handled. It prints the correct action for each light color.
enum Light { case red, yellow, green } func action(for light: Light) { switch light { case .red: print("Stop") case .yellow: print("Get ready") case .green: print("Go") } } action(for: .red) action(for: .yellow) action(for: .green)
If you forget a case in a switch on an enum, Swift will give a compile error.
You can use default to cover all other cases, but it's better to list all enum cases explicitly for clarity.
Exhaustive switches help catch errors early and make your code safer.
Swift switch statements must cover all possible cases.
Use explicit cases or a default to be exhaustive.
This rule helps prevent bugs and makes code easier to understand.
Practice
switch statement must be exhaustive?Solution
Step 1: Understand the meaning of exhaustive switch
In Swift, a switch must cover every possible value of the variable it checks to avoid missing cases.Step 2: Compare options to definition
Only It must handle all possible values of the variable being switched on. states that all possible values must be handled, which matches the requirement.Final Answer:
It must handle all possible values of the variable being switched on. -> Option AQuick Check:
Exhaustive means all cases covered = A [OK]
- Thinking switch can skip cases
- Believing only first match matters
- Assuming minimum number of cases
switch statements is syntactically correct and exhaustive for an enum Direction with cases north, south, east, and west?Solution
Step 1: Check all enum cases covered
The enum has four cases: north, south, east, west. The switch must cover all or use default.Step 2: Analyze each option
switch dir { case .north: print("North") case .south: print("South") case .east: print("East") case .west: print("West") } covers all four cases explicitly, making it exhaustive and syntactically correct.Final Answer:
switch with all four cases covered explicitly -> Option BQuick Check:
All enum cases covered = D [OK]
- Missing some enum cases
- Not using default when cases are incomplete
- Assuming partial cases are enough
enum Mood { case happy, sad, neutral }
let todayMood = Mood.sad
switch todayMood {
case .happy: print("Feeling great!")
case .sad: print("Feeling down.")
case .neutral: print("Just okay.")
}Solution
Step 1: Identify the value of todayMood
todayMood is set to Mood.sad.Step 2: Match the switch case
The switch has a case for .sad that prints "Feeling down." so this case runs.Final Answer:
Feeling down. -> Option CQuick Check:
Value sad matches case sad = B [OK]
- Confusing cases and printing wrong output
- Thinking switch is not exhaustive here
- Ignoring the actual value assigned
Color with cases red, green, and blue:switch color {
case .red: print("Red")
case .green: print("Green")
}Solution
Step 1: Check all enum cases handled
The enum has three cases: red, green, blue. The switch only handles red and green.Step 2: Identify missing coverage
Since .blue is missing and no default case is provided, the switch is not exhaustive, causing a compile error.Final Answer:
Missing case for .blue or default case, so switch is not exhaustive. -> Option DQuick Check:
All cases must be covered or default = C [OK]
- Ignoring missing enum cases
- Thinking parentheses needed after switch
- Confusing raw values with case coverage
enum Status { case success, failure, unknown }
let currentStatus = Status.unknown
switch currentStatus {
case .success: print("Success")
case .failure: print("Failure")
}Solution
Step 1: Understand the problem with current switch
The switch handles .success and .failure but misses .unknown, so it's not exhaustive.Step 2: Find a way to cover missing cases without listing all explicitly
Adding a default case covers any cases not explicitly handled, making the switch exhaustive.Final Answer:
Add a default case to handle all other values. -> Option AQuick Check:
Use default to cover missing cases = A [OK]
- Thinking you must list every case explicitly
- Removing switch instead of fixing it
- Changing enum just to fix switch
Ready to prove it?
Pass the test and this pattern moves unknown
