Swift BasicsLesson 16Free preview
Swift Switch
This lesson is part of Swift · All levels
Summary
This lesson reviews the 'switch' statement in Swift which is much more sophisticated and secure than the switch in C. Switches in Swift require full coverage of possible values (exhaustive), have no implicit fall-through behavior, and support advanced pattern matching such as range intervals and tuple.
Key points
- The 'switch' statement matches the values to some of the 'case' branches
- The switch on Swift is exhaustive: all possible values must be accommodated
- The default clause is mandatory if all possible values are not covered
- There is no fall-through automatically, so the keyword 'break' is not mandatory at the end of each case
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.