O'Reilly logo

Learning Swift by Andrew J Wagner

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

Enumerations

So far, we covered two of the three type classifications in Swift: structure and class. The third classification is called enumeration. Enumerations are used to define a group of related possible values that an instance can be. For example, if we want to represent the values of one of the three primary colors, an enumeration would be a great tool to do so.

Basic declaration

An enumeration is made up of cases much like a switch case and uses the keyword enum instead of struct or class. An enumeration for primary colors would look like this:

enum PrimaryColor {
    case Red
    case Green
    case Blue
}

You can then define a variable with this type and assign it one of the cases:

var color = PrimaryColor.Green

Note that to use one of the values, we ...

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