Basic Enums in Swift

From the Swift cheat sheet · Enums · verified Jul 2026

Basic Enums

Simple and raw value enumerations

swift
// Simple enum
enum Direction {
    case north, south, east, west
}

// Using enums
var heading = Direction.north
heading = .south  // Type inference

// Switch with enum
switch heading {
case .north:
    print("Going north")
case .south:
    print("Going south")
case .east, .west:
    print("Going east or west")
}

// Raw values
enum Planet: Int {
    case mercury = 1, venus, earth, mars
}

let earth = Planet.earth
print(earth.rawValue)  // 3
💡 Enums define a common type for related values
⚡ CaseIterable provides automatic allCases array
📌 Raw values must be literals and unique
🟢 Enums can have computed properties and methods

More Swift tasks

Back to the full Swift cheat sheet