0
0
Swiftprogramming~10 mins

Switch must be exhaustive in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Switch must be exhaustive
Start switch with value
Check each case
Execute case body
All cases covered?
NoError: Must be exhaustive
Yes
End
The switch checks each case for a match and must cover all possible values, otherwise it causes an error.
Execution Sample
Swift
enum Direction {
    case north, south, east, west
}

let dir = Direction.north

switch dir {
case .north: print("Up")
case .south: print("Down")
case .east: print("Right")
case .west: print("Left")
}
This code switches over all Direction enum cases and prints a direction word.
Execution Table
StepSwitch ValueCase CheckedMatch?ActionOutput
1Direction.north.northYesExecute print("Up")Up
2Direction.north.southNoSkip
3Direction.north.eastNoSkip
4Direction.north.westNoSkip
5-All cases checkedYesSwitch ends
💡 All enum cases are covered, so switch is exhaustive and ends normally.
Variable Tracker
VariableStartAfter Step 1After Step 5
dirDirection.northDirection.northDirection.north
Key Moments - 3 Insights
Why does Swift require all enum cases to be handled in a switch?
Swift wants to make sure you don't forget any case, so the switch must be exhaustive as shown in execution_table row 5.
What if I leave out one case in the switch?
The code will not compile because the switch is not exhaustive, meaning not all enum cases are covered.
Can I use a default case to make the switch exhaustive?
Yes, a default case covers all remaining cases and makes the switch exhaustive if you don't want to list all explicitly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which case matches the switch value at step 1?
A.south
B.north
C.east
D.west
💡 Hint
Check the 'Case Checked' and 'Match?' columns at step 1 in the execution_table.
At which step does the switch confirm it is exhaustive?
AStep 1
BStep 3
CStep 5
DStep 4
💡 Hint
Look at the 'All cases checked' row in the execution_table.
If the case .west was missing, what would happen?
AThe switch would not compile due to missing case.
BThe switch would cause a runtime error.
CThe switch would still compile and run.
DThe switch would print nothing.
💡 Hint
Refer to key_moments about missing cases causing compile errors.
Concept Snapshot
Switch statements in Swift must cover all possible values of the switched variable.
For enums, this means all cases must be handled.
If not all cases are listed, the compiler gives an error.
You can use a default case to cover any missing cases.
This ensures safe and predictable code execution.
Full Transcript
In Swift, when you use a switch statement with an enum, you must cover every possible case. The switch checks each case one by one. If the value matches a case, it runs that case's code. If you forget a case, the compiler will stop you because the switch is not exhaustive. You can add a default case to handle any cases you don't list explicitly. This rule helps prevent bugs by making sure you think about every possible value.