Enumerations in Swift

Enumerations, or enums, are a powerful feature in Swift that allow you to define a common type for a group of related values.

article

Understanding Enumerations in Swift

Enumerations, or enums, are a powerful feature in Swift that allow you to define a common type for a group of related values. They enable you to work with those values in a type-safe way within your code. Enums can also have associated values and methods, making them versatile for various use cases. In this article, we will explore what enumerations are, how they work, and provide examples to illustrate their usage in Swift.


What are Enumerations?


Overview

Enumerations define a type consisting of a group of related values and enable you to work with those values in a type-safe manner. Each value defined in an enumeration is a distinct case of that enumeration type.


Example of a Simple Enumeration:

enum CompassPoint {
    case north
    case south
    case east
    case west
}
var direction = CompassPoint.north
direction = .south

Associated Values


Overview

Enums can have associated values that store additional information for each case. This allows each case to carry a different set of values and types as associated data.


Example of Enumerations with Associated Values:

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

Raw Values


Overview

Enums can also define raw values, which are default values for each case. Raw values can be strings, characters, or any integer or floating-point number. Each raw value must be unique within its enumeration.


Example of Enumerations with Raw Values:

enum Planet: Int {
    case mercury = 1
    case venus
    case earth
    case mars
}
let earthOrder = Planet.earth.rawValue
let possiblePlanet = Planet(rawValue: 3)

Methods in Enumerations


Overview

Enums can also define methods to provide additional functionality related to the values they represent. This allows you to encapsulate behavior directly within the enum type.


Example of Enumerations with Methods:

enum CompassPoint {
    case north, south, east, west
func description() -> String {
    switch self {
    case .north:
        return "North"
    case .south:
        return "South"
    case .east:
        return "East"
    case .west:
        return "West"
    }
}
}

let direction = CompassPoint.east
print(direction.description()) // Output: East

Benefits of Using Enumerations


Type Safety and Clarity

Enums provide type safety by restricting values to the predefined cases, reducing the risk of invalid values. They also improve code clarity by making the code more readable and self-documenting.


Example of Type Safety and Clarity:

enum Direction {
    case north
    case south
    case east
    case west
}
func move(to direction: Direction) {
switch direction {
case .north:
print("Moving north")
case .south:
print("Moving south")
case .east:
print("Moving east")
case .west:
print("Moving west")
}
}

move(to: .north) // Output: Moving north

Enumerations in Swift are a versatile and powerful feature that provide type safety, clarity, and the ability to work with related values in a structured way. By using enums with associated values, raw values, and methods, you can write more expressive and maintainable code. Understanding and utilizing enums effectively will enhance your Swift programming skills and help you build more robust applications.


instructor

Exodai INSTRUCTOR!

Johan t'Sas

Owner and Swift developer!