April 2015
Intermediate to advanced
556 pages
17h 47m
English
An enumeration (“enum” for short) 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
When the compiler expects the enum type, you can skip the full type name. For example:
var piesToBake: [PieType] = [] piesToBake.append(.Apple)
Swift has a powerful switch statement which, among other things, is great for matching on enum values:
let name: String
switch favoritePie {
case .Apple:
name = "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, ...
Read now
Unlock full access