O'Reilly logo

Protocol-Oriented Programming with Swift by Jon Hoffman

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

The enumerations

In most languages, enumerations are little more than a data type consisting of a set of named values called elements. In Swift, however, the enumerations have been supercharged to give them significantly more power. Enumerations in Swift are a lot closer in functionality to classes and structures; however, they can still be used like enumerations in other languages.

Before we see how enumerations are supercharged in Swift, let's see how we can use them as standard enumerations. The following code defines an enumeration called Devices:

enum Devices {
  case IPod 
  case IPhone 
  case IPad 
}

In the Devices enumeration, we defined three possible values: IPod, IPhone, and IPad.

One of the items that make enumerations different in Swift as compared ...

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