Skip to main content

Enumerations

An enumeration defines a finite set of named cases under a common type. Use the enum keyword followed by a name and a list of cases.

Declaration

enum Direction {
north,
south,
east,
west,
}

Using Enum Values

Refer to a case by prefixing it with the enum type name:

var heading = Direction.north

Comparing Enum Values

Enum values support equality comparison with == and !=:

if heading == Direction.north {
print("Going north")
}

Enums in String Interpolation

When interpolated into a string, an enum value produces its case name:

print("Direction: \{heading}")  // Prints "Direction: north"