Swift Enums

In this article, we will explain enums (enumerations) in Swift.

What Are Enums in Swift?

Enums in Swift are a way to group related values together under a single type.

When coding, if the possible values for a variable or function parameter are limited, it is better to define an enum instead of just using a String. This makes the code clearer and helps prevent errors.


For example, suppose you want a function move() that accepts one of up, down, left, or right as a parameter.

You could use String as the parameter type, but then a typo like "rigth" would not cause an error. Defining an enum makes the code safer and easier to understand.


Defining Enums in Swift

In Swift, you define an enum using the keywords enum and case like this:

enum [EnumName] {
    case [case1]
    case [case2]
    case [case3]
}

Unlike C or Objective-C, Swift does not automatically assign integer values to enum cases.


For example, you can define an enum called Direction for the move() function like this:

enum Direction {
    case up
    case down
    case left
    case right
}

You can also define it in one line:

enum Direction {
    case up, down, left, right
}

When assigning or comparing enum values, you can use EnumName.case, or simply .case when the type is already known:

var d = Direction.right
d = .left

Using Enums in switch Statements

Enums in Swift are often used with switch statements.

For example, here’s how you can switch over a variable d of type Direction and print a message for each case:

enum Direction {
    case up
    case down
    case left
    case right
}

let d = Direction.right

switch d {
case .up:
    print("Direction is up")
case .down:
    print("Direction is down")
case .left:
    print("Direction is left")
case .right:
    print("Direction is right")
}

The output will be:

Direction is right

Passing Enums as Parameters

You can also use enums as function parameters by specifying the enum name as the type.

For example, here’s the previous code refactored into a function move() that takes Direction as a parameter:

enum Direction {
    case up
    case down
    case left
    case right
}

func move(direction: Direction) {

    switch direction {
    case .up:
        print("Direction is up")
    case .down:
        print("Direction is down")
    case .left:
        print("Direction is left")
    case .right:
        print("Direction is right")
    }
}

move(direction: .down)

The output will be:

Direction is down

Using Enums with Associated Values

In Swift, enums can also store additional data by defining associated values.

For example, here’s how you could give each Direction case an Int indicating how far to move:

In a switch, you can extract the value with case .case(let constant).

enum Direction {
    case up(Int)
    case down(Int)
    case left(Int)
    case right(Int)
}

let direction = Direction.down(2)

switch direction {
case .up(let i):
    print("Direction is up - \(i)")
case .down(let i):
    print("Direction is down - \(i)")
case .left(let i):
    print("Direction is left - \(i)")
case .right(let i):
    print("Direction is right - \(i)")
}

The output will be:

Direction is down - 2

You can assign multiple associated values, and their types can vary by case.


Using Enums with Raw Values

As mentioned earlier, unlike C or Objective-C, Swift enums do not have integer values assigned by default.

If you want to assign values like Int or String to cases, you can use raw values.


When defining the enum, specify the type after the name (e.g. enum Direction: Int), then assign values with case caseName = rawValue.

You can then access them with .rawValue.


enum Direction1: Int {
    case up = 0
    case down
    case left
    case right
}

let d1 = Direction1.left
print(d1.rawValue)

enum Direction2: String {
    case up = "UP"
    case down = "DOWN"
    case left = "LEFT"
    case right = "RIGHT"
}

let d2 = Direction2.left
print(d2.rawValue)

The output will be:

2
LEFT

That wraps up our explanation of enums in Swift.