April 2020
Intermediate to advanced
528 pages
15h 40m
English
An enumeration is a type with a discrete set of values. Define an enum describing pies:
enum PieType {
case apple
case cherry
case pecan
}
let favoritePie = PieType.apple apple
Swift has a powerful switch statement that, among other things, is great for matching on enum values:
let name: String
switch favoritePie {
case .apple:
name = "Apple" "Apple"
case .cherry:
name = "Cherry"
case .pecan:
name = "Pecan"
}
The cases for a switch statement must be exhaustive: Each possible value of the switch expression must be accounted for, whether explicitly or via a default case. Unlike in C, Swift switch cases do not fall through – only the code for the case that is matched is executed. (If ...
Read now
Unlock full access