O'Reilly logo

Application Development with Swift by Hossam Ghareeb

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

Enum

Enumeration is a very useful concept that is used to group related values together and define a new type for them. You must be familiar with enumeration in Objective-C or in C. Swift added new flavors to enum, and made it more flexible and easy to use.

To create enum in Swift, use the enum keyword, and then you can list all the possible cases after the case keyword:

enum LevelDifficulty{
    case Easy
    case Medium
    case Hard
    case Advanced
}

In the preceding code, we defined the new type as LevelDifficulty to group the related values of difficulties together (Easy, Medium, Hard, and Advanced). To use this enum, you can easily create variables with the LevelDifficulty type:

var easyMode = LevelDifficulty.Easy //Type is inferred as LevelDifficulty var ...

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