Swift Introduction | Structures, Enumerations, and Tuples | Enumerations

An enum, or enumeration, is used when selecting one value from multiple possibilities.

Define an enumeration with the enum keyword.

enum Name {
    case value1
    case value2
    ... omitted ...
}

Specify the name after enum, then declare the available values with case inside the braces. You can also declare multiple values after one case.

enum Name {
    case value1, value2, ...
}

Use an enumeration value in the form Name.value.

An enumeration can also define raw values.

enum Name : Type {
    case value1 = rawValue
    case value2 = rawValue
    ...
}

Specify the raw value type after the enumeration name, then assign a raw value to each case with an equals sign (=). Retrieve it with the rawValue property.

enum Janken {
    case Choki
   case Goo
    case Paa
}

enum RockPaperScissors : String {
    case Choki = "scissors"
    case Goo = "rock"
    case Paa = "paper"
}

var me = Janken.Goo
var you = RockPaperScissors.Goo
print(me)
print(you.rawValue)

This example defines and uses two enumerations. RockPaperScissors assigns a String raw value to each case.

Printing Janken.Goo displays Goo, while printing RockPaperScissors.Goo.rawValue displays rock.