100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Enumerations in Swift

Learn how Swift enums define related values, support associated and raw values, and integrate with switch statements.

Error Handling & ConcurrencyBeginner8 min readJul 8, 2026
Analogies

Introduction

An enumeration (enum) defines a common type for a group of related values, letting you work with those values in a type-safe way throughout your code. Unlike simple C-style enums, Swift enumerations are first-class types: they can have associated values attached to each case, raw values backing each case, computed properties, and even instance methods, making them far more powerful than a simple list of named constants.

🏏

Cricket analogy: A DismissalType enum grouping bowled, caught, and run-out into one type-safe value is like an umpire's official ruling categories, each of which can carry associated details such as the fielder's name for a catch.

Syntax

swift
enum Direction {
    case north
    case south
    case east
    case west
}

let heading = Direction.north

switch heading {
case .north:
    print("Heading north")
case .south:
    print("Heading south")
case .east:
    print("Heading east")
case .west:
    print("Heading west")
}

Explanation

Direction defines four related cases that can be written on a single line separated by commas, or one per line. A switch statement over an enum must be exhaustive, covering every case (or including a default clause), which the compiler enforces — a major safety benefit when new cases are added later. Enums can go beyond simple named cases: associated values let each case carry extra data of its own, while raw values give every case an underlying literal value of a shared type, useful for things like mapping to serialized data.

🏏

Cricket analogy: A FieldingPosition enum with slip, gully, cover, and midwicket written on one line covers four cases, and a switch statement assigning fielders must handle every position or the compiler flags it, just as a captain can't leave a gap in the field.

Example

swift
// Associated values: each case can store different data
enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}

let productCode = Barcode.upc(8, 85909, 51226, 3)

switch productCode {
case .upc(let numberSystem, let manufacturer, let product, let check):
    print("UPC: \(numberSystem), \(manufacturer), \(product), \(check)")
case .qrCode(let productCode):
    print("QR code: \(productCode)")
}

// Raw values: every case shares a backing type
enum Planet: Int {
    case mercury = 1, venus, earth, mars
}

let earthOrder = Planet.earth.rawValue
print("Earth is planet number \(earthOrder)")

Output

swift
UPC: 8, 85909, 51226, 3
Earth is planet number 3

Key Takeaways

  • Swift enums are first-class types that can carry associated values, raw values, methods, and computed properties.
  • Associated values let each case store different, case-specific data (e.g. Barcode.upc(Int, Int, Int, Int)).
  • Raw values give every case a literal value of a shared type, accessible via .rawValue and initializable via Type(rawValue:).
  • switch statements over an enum must be exhaustive, which the compiler checks at build time.
  • Enums are value types, so they are copied on assignment and are not managed by ARC.
  • Enums are the idiomatic way to model a closed set of related states or options, including error types.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#EnumerationsInSwift#Enumerations#Syntax#Explanation#Example#StudyNotes#SkillVeris