Chapter    10

Enumerations

Enumeration is another type where you can group items of related values and use them in a type safe way in your code. In C/Objective-C you only had enumerations of type integer. But in Swift, enumerations have gotten quite an overhaul. You can define enumeration of type String, Character, Int, or a floating type; these also include variants of the types such as Double, UInt, etc.

Enumerations in Swift are first-class citizens, similar to Classes and Structures. They can do the following:

  • Implement computed properties
  • Instance methods
  • Custom initializers to provide initial values
  • Be extended
  • Conform to protocols

Syntax

The basic syntax is the keyword enum and is then followed by the name of the enumeration. Notice ...

Get Learn Swift 2 on the Mac, Second Edition 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.