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

Get Swift Quick Syntax Reference 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.