Why does Swift require switch statements on enums to be exhaustive?
hard📝 Conceptual Q10 of 15
Swift - Control Flow
Why does Swift require switch statements on enums to be exhaustive?
ABecause Swift does not support default cases.
BTo ensure all possible cases are handled, preventing runtime errors.
CTo make code shorter and easier to write.
DTo allow switching on any type, not just enums.
Step-by-Step Solution
Solution:
Step 1: Understand Swift's safety goals
Swift aims to prevent runtime errors by forcing all enum cases to be handled.
Step 2: Analyze options
To ensure all possible cases are handled, preventing runtime errors. correctly states that exhaustiveness ensures all cases are handled, avoiding runtime errors.
Final Answer:
To ensure all possible cases are handled, preventing runtime errors. -> Option B