Swift Enumerations

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
May 19, 2023

Working with Swift Enumerations

If you are familiar with Swift, then you might already know that it makes extensive use of enumerations. In Swift, enumerations are a way to define a group of related values. You can define an enumeration using the enum keyword and then specify the cases that belong to the enumeration. The cases can be associated with values of any type, including integers, strings, and even other enumerations.

Defining an Enumeration

Here is an example of defining an enumeration in Swift:

enum Directions {
    case north
    case south
    case east
    case west
}

In this example, we have defined an enumeration called Directions with four cases: north, south, east, and west. Each case represents a specific value for the enumeration.

Using an Enumeration

Once an enumeration is defined, you can use it to create variables, constants, and function parameters. Here is an example of using the Directions enumeration:

var currentDirection: Directions = .north}

In this example, we have created a currentDirection variable of the Directions enumeration type. We have initialized it with the north case using the dot syntax. The dot syntax is used to access the cases of an enumeration.

You can also use a switch statement to match against the cases of an enumeration:

switch currentDirection {
case .north:
    print("Going north")
case .south:
    print("Going south")
case .east:
    print("Going east")
case .west:
    print("Going west")
}

In this example, we are using a switch statement to print a message based on the value of currentDirection.

Associated Values

One of the powerful features of Swift enumerations is the ability to associate values with each case. Here is an example of an enumeration with associated values:

enum Temperature {
    case celsius(Double)
    case fahrenheit(Double)
}

In this example, we have defined an enumeration called Temperature with two cases: celsius and fahrenheit. The celsius case has an associated value of type Double representing the temperature in Celsius, and the fahrenheit case has an associated value of type Double representing the temperature in Fahrenheit.

You can use an enumeration with associated values like this:

let currentTemperature = Temperature.celsius(23.5)

switch currentTemperature {
case .celsius(let temperature):
    print("The temperature in Celsius is \(temperature)")
case .fahrenheit(let temperature):
    print("The temperature in Fahrenheit is \(temperature)")
}

In this example, we have created a currentTemperature constant of the Temperature enumeration type. We have initialized it with the celsius case and an associated value of 23.5. We are using a switch statement to print a message based on the value of currentTemperature.

Raw Values

Swift enumerations also support raw values, which are values that are pre-populated for each case. Here is an example of an enumeration with raw values:

enum Planet: String {
    case mercury = "Mercury"
    case venus = "Venus"
    case earth = "Earth"
    case mars = "Mars"
    case jupiter = "Jupiter"
    case saturn = "Saturn"
    case uranus = "Uranus"
    case neptune = "Neptune"
}

In this example, we have defined an enumeration called Planet with eight cases, each with a raw value of type String. The raw values are explicitly assigned in the enumeration definition.

You can access the raw value of an enumeration case like this:

let earth = Planet.earth
print(earth.rawValue)}

In this example, we have created a constant called earth and initialized it with the earth case of the Planet enumeration. We are using the rawValue property to print the raw value of the earth case, which is "Earth".

Summary

In Swift, enumerations are a powerful way to define a group of related values. You can use enumerations to create variables, constants, and function parameters, and switch statements to match against the cases of an enumeration. Swift enumerations also support associated values and raw values, which can be used to associate additional values with each case or pre-populate values for each case.