Enumerations
Enumerations are sets of symbolic names bound to unique, constant values, which can be compared by identity.
Enum declaration
Enums are declared using the enum
keyword, followed by the name of the enum, the raw type after a colon, and the requirements, which must be enclosed in opening and closing braces.
The raw type must be an integer subtype (e.g., UInt8
or Int128
).
Enum cases are declared using the case
keyword, followed by the name of the enum case.
Enum cases must be unique. Each enum case has a raw value, which is the index of the case among all cases.
The raw value of an enum case can be accessed through the rawValue
field.
The enum cases can be accessed by using the name as a field on the enum or by using the enum constructor, which requires providing the raw value as an argument. The enum constructor returns the enum case with the given raw value, if any, or nil
if no such case exists.
Enum cases can be compared using the equality operators ==
and !=
.
Working with an enum declaration
-
Declare an enum named
Color
, which has the raw value typeUInt8
, and declare three enum cases (red
,green
, andblue
):_12access(all)_12enum Color: UInt8 {_12_12access(all)_12case red_12_12access(all)_12case green_12_12access(all)_12case blue_12} -
Declare a variable that has the enum type
Color
and initialize it to the enum caseblue
of the enum:_10let blue: Color = Color.blue -
Get the raw value of the enum case
blue
. Since it is the third case, it has an index of 2:_10blue.rawValue // is `2` -
Get the
green
enum case of the enumColor
by using the enum constructor and providing the raw value of the enum casegreen
, 1. Since the enum casegreen
is the second case, it has an index of 1:_10let green: Color? = Color(rawValue: 1) // is `Color.green` -
Get the enum case of the enum
Color
with the raw value 5. As there are only three cases, the maximum raw value/index is 2:_10let nothing = Color(rawValue: 5) // is `nil`Enum cases can be compared:
_10Color.red == Color.red // is `true`_10Color(rawValue: 1) == Color.green // is `true`Different enum cases are not the same:
_10Color.red != Color.blue // is `true`