Enumerations & Associated Values
Enums model a fixed set of possibilities and are far more powerful in Swift than in many languages. By the end of this lesson you'll define enum s, attach raw and associated values, switch over cases exhaustively, and iterate with CaseIterable .
Learn Enumerations & Associated Values in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Defining Enums & Switching
Declare an enum with enum and list its case s. When the type is known, you can write just .north . A switch over an enum must be exhaustive — the compiler ensures you handle every case.
2️⃣ Raw Values
Give cases a fixed underlying value by writing the raw type, e.g. enum Planet: Int . Read it with .rawValue , and create a case from a raw value with the failable init(rawValue:) , which returns an optional.
3️⃣ Associated Values
Associated values let each case carry extra data that can differ per instance. Extract that data in a switch with case let pattern binding.
4️⃣ Iterating with CaseIterable
Conform to CaseIterable and Swift gives you allCases — every case in order. Ideal for menus, pickers, and validation.
Your turn. Fill in the blanks, then run it and check the output.
📋 Quick Reference
Build it, run it, and check your output against the example in the comments.
Practice quiz
What keyword defines an enumeration in Swift?
- enumeration
- case
- enum
- type
Answer: enum. Enumerations are declared with the enum keyword.
What does an enum represent?
- A fixed set of related, named cases
- A list that can grow
- A key-value map
- An optional value
Answer: A fixed set of related, named cases. An enum defines a group of related values as named cases.
After , what is short for?
- north only
- a String
- a number
- Direction.north
Answer: Direction.north. Swift infers the type, so .north means Direction.north.
What is a RAW VALUE in an enum?
- A random number
- A fixed underlying value (e.g. Int or String) tied to each case
- An associated value
- The case name only
Answer: A fixed underlying value (e.g. Int or String) tied to each case. Raw values are constant underlying values like Int or String attached to each case.
What does an ASSOCIATED VALUE let a case do?
- Carry extra data that varies per instance
- Store nothing
- Become a class
- Repeat itself
Answer: Carry extra data that varies per instance. Associated values let each case hold extra, instance-specific data.
Which raw-typed enum initializer can fail and returns an optional?
- Direction(name:)
- MyEnum.init()
- MyEnum(rawValue:)
- MyEnum.first
Answer: MyEnum(rawValue:). init(rawValue:) returns an optional because the raw value might not match any case.
What does conforming to CaseIterable provide?
- A description property
- An allCases collection of every case
- Automatic raw values
- JSON encoding
Answer: An allCases collection of every case. CaseIterable synthesizes allCases, a collection of all the enum's cases.
Why is a switch over an enum often safer than over an Int?
- It's faster
- It uses less memory
- It avoids loops
- The compiler requires you handle every case (exhaustiveness)
Answer: The compiler requires you handle every case (exhaustiveness). Switches over enums must be exhaustive, so the compiler catches missing cases.
How do you extract an associated value in a switch case?
- case .point: x
- case let .point(x, y):
Answer: case let .point(x, y):. Bind associated values with patterns like case let .point(x, y).
If a raw-value enum has Int raw values starting implicitly, what is the first case's raw value by default?
- 1
- -1
- 0
- nil
Answer: 0. For an Int-backed enum, implicit raw values start at 0 and increment.