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
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
// 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
UPC: 8, 85909, 51226, 3
Earth is planet number 3Key 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
1. What keyword introduces an enumeration definition in Swift?
2. What are associated values in a Swift enum?
3. How do you access the raw value of an enum case with a raw value type?
4. What must a switch statement over a Swift enum do, unless it has a default case?
5. Which of the following capabilities do Swift enums support that simple C-style enums do not?
Was this page helpful?
You May Also Like
Structs in Swift
Structs are value types in Swift that are copied on assignment and come with a free memberwise initializer.
switch Statement in Swift
Explore Swift's powerful switch statement, which requires exhaustiveness and never falls through by default.
Error Handling in Swift
Learn how Swift represents, throws, propagates, and handles runtime errors using the Error protocol and try/catch.
Data Types in Swift
Explore Swift's core data types—Int, Double, Float, Bool, String, Character, and tuples—and how Swift handles numeric type safety.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics