switch — exhaustiveness, ranges, tuples, where
Use powerful switch with exhaustive cases, numeric ranges, tuple matching, and where clauses.
Intro
switch in Swift is expressive and safe: it is exhaustive and supports ranges, tuples, and where clauses.
Basic switch
Switch can match strings and must be exhaustive via cases or default.
let fruit = "apple"
switch fruit {
case "apple": print("Good fiber")
case "banana": print("Potassium")
default: print("Tasty fruit")
}All lessons in this course
- if/else basics
- switch — exhaustiveness, ranges, tuples, where
- Loops — for-in, while, repeat-while