Search⌘ K
AI Features

Swift Enumerations

Explore how to use Swift enumerations to create predefined sets of values that help manage app states and decisions. Understand enum cases, associated values, and how to implement them in switch statements to write safer and clearer code for mobile apps.

An overview of enumerations

Enumerations (typically referred to as enums) are used to create custom data types consisting of predefined sets of values. Enums are typically used for making decisions within code such as when using switch statements. An enum might, for example, be declared as follows:

enum Temperature {
    case hot
    case warm
    case cold
}

Note that in this example, none of the cases are assigned a value. An enum of this type is essentially used to reference one of a pre-defined set of states (in this case the current temperature being hot, ...