What does it mean for a switch statement to be exhaustive in Swift?
It means the switch must cover all possible cases of the value being checked, so no case is left unhandled.
Click to reveal answer
beginner
Why does Swift require switch statements to be exhaustive?
To ensure safety by forcing the programmer to handle every possible value, preventing unexpected behavior at runtime.
Click to reveal answer
beginner
How can you make a switch statement exhaustive when you don't want to handle all cases explicitly?
You can add a default case to catch all remaining values.
Click to reveal answer
intermediate
Given an enum with 3 cases, what happens if you write a switch statement that only handles 2 cases without a default?
The code will not compile because the switch is not exhaustive; the missing case must be handled or a default added.
Click to reveal answer
intermediate
Can switch statements on non-enum types be exhaustive in Swift?
Yes, but only by using a default case because non-enum types can have many values.
Click to reveal answer
What keyword can you use to make a switch statement exhaustive without listing all cases?
Adefault
Belse
Ccatch
Dfinal
✗ Incorrect
The default case catches all values not explicitly handled, making the switch exhaustive.
If you have an enum with 4 cases, how many cases must your switch statement handle to be exhaustive?
AAll 4 or use default
BNone, switch is always exhaustive
COnly 1
DAt least 3
✗ Incorrect
You must handle all 4 cases explicitly or add a default case to cover any unhandled cases.
What happens if a switch statement on an enum is not exhaustive in Swift?
AIt compiles but crashes at runtime
BIt compiles with a warning
CIt does not compile
DIt runs but skips missing cases
✗ Incorrect
Swift requires exhaustive switches on enums, so missing cases cause a compile-time error.
Can you omit the default case if your switch covers all enum cases?
ANo, default is always required
BYes, if all cases are covered
COnly if enum has less than 3 cases
DOnly for integer types
✗ Incorrect
If all enum cases are explicitly handled, the switch is exhaustive without a default.
Why might you prefer to list all enum cases explicitly instead of using default?
ATo avoid runtime errors
BTo make code shorter
CTo improve performance
DTo get compiler errors if new cases are added later
✗ Incorrect
Explicit cases help catch missing cases when enums change, improving code safety.
Explain what it means for a switch statement to be exhaustive in Swift and why it is important.
Think about how Swift prevents missing cases in switch.
You got /4 concepts.
Describe how you can make a switch statement exhaustive when working with enums and when working with other types.
Consider differences between enums and other types.
You got /3 concepts.
Practice
(1/5)
1. What does it mean when we say a Swift switch statement must be exhaustive?
easy
A. It must handle all possible values of the variable being switched on.
B. It can skip some cases if they are unlikely to happen.
C. It only needs to handle the first case it matches.
D. It must have at least two cases.
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 A
Quick Check:
Exhaustive means all cases covered = A [OK]
Hint: Remember: switch covers all cases or uses default [OK]
Common Mistakes:
Thinking switch can skip cases
Believing only first match matters
Assuming minimum number of cases
2. Which of the following Swift switch statements is syntactically correct and exhaustive for an enum Direction with cases north, south, east, and west?
easy
A. switch dir { case .north: print("North") case .south: print("South") }
B. switch dir { case .north: print("North") case .south: print("South") case .east: print("East") case .west: print("West") }
C. switch dir { case .north: print("North") case .south: print("South") case .east: print("East") }
D. switch (dir) { case .north: print("North") case .south: print("South") default: print("Other") }
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 B
Quick Check:
All enum cases covered = D [OK]
Hint: Cover all enum cases or use default to be exhaustive [OK]
Common Mistakes:
Missing some enum cases
Not using default when cases are incomplete
Assuming partial cases are enough
3. What will be the output of this Swift code?
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.")
}
medium
A. Feeling great!
B. Just okay.
C. Feeling down.
D. Compile error: switch not exhaustive
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 C
Quick Check:
Value sad matches case sad = B [OK]
Hint: Match variable value to switch case to find output [OK]
Common Mistakes:
Confusing cases and printing wrong output
Thinking switch is not exhaustive here
Ignoring the actual value assigned
4. Identify the error in this Swift switch statement for enum Color with cases red, green, and blue:
switch color {
case .red: print("Red")
case .green: print("Green")
}
medium
A. Cannot use enum cases without raw values.
B. Syntax error: missing parentheses after switch.
C. No error, this switch is correct.
D. Missing case for .blue or default case, so switch is not exhaustive.
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 D
Quick Check:
All cases must be covered or default = C [OK]
Hint: Always cover all enum cases or add default [OK]
Common Mistakes:
Ignoring missing enum cases
Thinking parentheses needed after switch
Confusing raw values with case coverage
5. Given this enum and switch, how can you make the switch exhaustive without listing all cases explicitly?
enum Status { case success, failure, unknown }
let currentStatus = Status.unknown
switch currentStatus {
case .success: print("Success")
case .failure: print("Failure")
}
hard
A. Add a default case to handle all other values.
B. Remove the switch and use if-else instead.
C. Add a case for .unknown explicitly.
D. Change enum to have only two cases.
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 A
Quick Check:
Use default to cover missing cases = A [OK]
Hint: Use default to cover all unlisted cases quickly [OK]
Common Mistakes:
Thinking you must list every case explicitly
Removing switch instead of fixing it
Changing enum just to fix switch
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.