Enums

Enums, structs, classes, and protocols are all written in a very similar way. Here is how you create an enum for the suits in a deck of cards:

enum Suit {    //... enum implementation goes here}

You should choose a singular name (not plural) for the enum—like Suit in this case. You write the word enum and then give the enum a name and write a pair of curly brackets. The enum implementation goes inside the curly brackets. Here is a simple enum that declares all possible suits in a deck of cards:

enum Suit {    case Hearts    case Clubs    case Diamonds    case Spades}

Now you can declare Suit.Clubs:

var thisCardSuit = Suit.Clubs

Now thisCardSuit is of type Suit. Each one of the ...

Get Learning Swift™ Programming 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.