Enumerations and the Switch Statement
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:
Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.