0
0
Swiftprogramming~10 mins

Iterating enum cases with CaseIterable in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Iterating enum cases with CaseIterable
Define enum with CaseIterable
Access allCases property
Start loop over allCases
Process each enum case
Loop ends when all cases processed
This flow shows how to define an enum with CaseIterable, then loop through all its cases using the allCases property.
Execution Sample
Swift
enum Direction: CaseIterable {
    case north, south, east, west
}

for dir in Direction.allCases {
    print(dir)
}
This code defines an enum Direction with four cases and prints each case by iterating over allCases.
Execution Table
Iterationdir (enum case)ActionOutput
1northPrint dirnorth
2southPrint dirsouth
3eastPrint direast
4westPrint dirwest
--Loop endsAll cases printed
💡 All enum cases have been iterated, loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
dir-northsoutheastwest-
Key Moments - 3 Insights
Why can we use Direction.allCases in the for loop?
Because the enum Direction conforms to CaseIterable, Swift automatically provides the allCases collection of all enum cases, as shown in the execution_table rows 1 to 4.
What happens if we add a new case to the enum?
The allCases collection automatically includes the new case, so the loop will iterate over it without any code change, as the execution_table would have an extra iteration.
Can we use allCases with enums that don't conform to CaseIterable?
No, only enums that conform to CaseIterable have the allCases property. Without it, the code would not compile, so the loop cannot run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of dir at iteration 3?
Aeast
Bsouth
Cnorth
Dwest
💡 Hint
Check the 'dir (enum case)' column at iteration 3 in the execution_table.
At which iteration does the loop end according to the execution_table?
AAfter iteration 3
BAfter iteration 5
CAfter iteration 4
DIt never ends
💡 Hint
Look at the exit_note and the last row of the execution_table.
If we add a new case 'up' to the enum, how would the execution_table change?
AThe loop would skip the new case
BIt would have one more iteration with dir = up
CThe code would not compile
DThe output would be empty
💡 Hint
Refer to key_moments about adding new cases and how allCases updates automatically.
Concept Snapshot
enum MyEnum: CaseIterable {
    case a, b, c
}

for item in MyEnum.allCases {
    // use item
}

- CaseIterable auto-creates allCases array
- Looping over allCases visits every enum case
- Adding cases updates allCases automatically
Full Transcript
This example shows how to use the CaseIterable protocol in Swift to loop over all cases of an enum. First, we define an enum Direction with cases north, south, east, and west, and make it conform to CaseIterable. This gives us access to Direction.allCases, a collection of all enum cases. Then, we use a for loop to go through each case in allCases and print it. The execution_table shows each iteration with the current enum case and the printed output. The variable_tracker shows how the variable dir changes each iteration. Key moments clarify why allCases works only with CaseIterable enums and how adding new cases affects the loop. The visual quiz tests understanding of the iteration steps and behavior when modifying the enum. The concept snapshot summarizes the syntax and behavior for quick reference.