O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

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    19

Enumerations

You use an enumeration (or an enum) to define a restricted set of values. Enums make your code clearer because you can use descriptive names instead of something abstract like an integer value.

If you wanted to define an enumeration to describe a machine state, you could do something like Listing 19-1.

Listing 19-1. Defining Enumerations

enum State {    case Inactive    case Active    case Hibernate    case Terminated}var machineState = State.Inactive

In Listing 19-1, you defined an enum named State that can have four values: Inactive, Active, Hibernate, and Terminated. You specify enum values with the case keyword. You can specify one value per case keyword, or you can provide a comma-separated list of enum values ...

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