O'Reilly logo

Learn Swift 2 on the Mac, Second Edition by Waqar Malik

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required