Iterating enum cases with CaseIterable in Swift - Time & Space Complexity
When we loop through all cases of an enum using CaseIterable, we want to know how the time needed grows as the number of cases increases.
We ask: How does the work change if the enum has more or fewer cases?
Analyze the time complexity of the following code snippet.
enum Direction: CaseIterable {
case north, south, east, west
}
for direction in Direction.allCases {
print(direction)
}
This code loops through all possible directions defined in the enum and prints each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each enum case in
Direction.allCases. - How many times: Once for each case in the enum.
As the number of enum cases grows, the loop runs once for each case.
| Input Size (n) | Approx. Operations |
|---|---|
| 4 | 4 (one print per case) |
| 10 | 10 |
| 100 | 100 |
Pattern observation: The work grows directly with the number of enum cases.
Time Complexity: O(n)
This means the time to run the loop grows in a straight line as the number of enum cases increases.
[X] Wrong: "Looping over enum cases is constant time because enums are fixed."
[OK] Correct: Even though enums have fixed cases, the loop still runs once per case, so time grows with the number of cases.
Understanding how looping over enum cases scales helps you explain your code's efficiency clearly and confidently in real-world coding discussions.
What if we nested another loop inside to print each case twice? How would the time complexity change?
Practice
CaseIterable protocol allow you to do with an enum in Swift?Solution
Step 1: Understand the purpose of CaseIterable
TheCaseIterableprotocol provides a way to access all cases of an enum as a collection.Step 2: Identify what CaseIterable does not do
It does not allow changing cases, adding stored properties, or changing access levels.Final Answer:
Access all enum cases as a collection -> Option BQuick Check:
CaseIterable = Access all cases [OK]
- Thinking CaseIterable allows modifying enum cases
- Confusing CaseIterable with adding properties
- Assuming CaseIterable changes access control
CaseIterable?Solution
Step 1: Check correct protocol conformance syntax
In Swift, you conform to a protocol by listing it after a colon without parentheses.Step 2: Identify the correct protocol name
The correct protocol isCaseIterable, notIterable, and no parentheses are used.Final Answer:
enum Direction: CaseIterable { case north, south, east, west } -> Option AQuick Check:
Correct syntax = enum Direction: CaseIterable { case north, south, east, west } [OK]
- Adding parentheses after CaseIterable
- Using wrong protocol name like Iterable
- Forgetting to conform to CaseIterable
enum Fruit: CaseIterable {
case apple, banana, cherry
}
for fruit in Fruit.allCases {
print(fruit)
}Solution
Step 1: Understand the for loop over allCases
The loop iterates over each case in the enum and prints it on a new line.Step 2: Recognize print behavior in Swift
Each call toprintoutputs the case name followed by a newline.Final Answer:
apple banana cherry -> Option CQuick Check:
Loop prints each case on separate lines [OK]
- Assuming all cases print on one line
- Expecting array output instead of separate lines
- Confusing enum case names with full type names
enum Color: CaseIterable {
case red, green, blue
}
for color in Color {
print(color)
}Solution
Step 1: Identify how to iterate over enum cases
You cannot iterate over the enum type itself; you must useallCasesproperty.Step 2: Understand CaseIterable usage
CaseIterable providesallCaseswhich returns a collection of all enum cases for iteration.Final Answer:
You must use Color.allCases to iterate, not Color directly -> Option DQuick Check:
Use allCases for iteration [OK]
- Trying to loop over enum type directly
- Thinking CaseIterable needs raw values
- Assuming enum must be a class
enum PaymentMethod: CaseIterable {
case cash, creditCard, paypal, applePay
}Which code snippet correctly prints each payment method with a number starting from 1?
Solution
Step 1: Use enumerated() to get index and value
Theenumerated()method returns pairs of (index, element) for the collection.Step 2: Adjust index to start from 1 and print
Since enumerated indexes start at 0, add 1 to show numbering from 1.Final Answer:
for (index, method) in PaymentMethod.allCases.enumerated() { print("\(index + 1). \(method)") } -> Option AQuick Check:
Use enumerated() + index+1 for numbering [OK]
- Trying to add 1 directly to enum case
- Indexing allCases with 1-based index (off by one)
- Looping over enum type instead of allCases
Ready to prove it?
Pass the test and this pattern moves unknown
