Enum declaration and cases in Swift - Time & Space Complexity
Enums group related values together in Swift. We want to see how the time to create or use enums changes as we add more cases.
How does the number of enum cases affect the work the program does?
Analyze the time complexity of the following code snippet.
enum Direction {
case north
case south
case east
case west
}
func printDirection(_ dir: Direction) {
switch dir {
case .north: print("Going north")
case .south: print("Going south")
case .east: print("Going east")
case .west: print("Going west")
}
}
This code defines an enum with four directions and a function that prints a message based on the direction.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch statement directly jumps to the matching enum case using a compiler-optimized jump table.
- How many times: Constant time (O(1)), independent of the number of enum cases.
As the number of enum cases grows, the switch execution time remains constant due to compiler optimizations like jump tables.
| Input Size (n) | Approx. Operations |
|---|---|
| 4 | 1 |
| 10 | 1 |
| 100 | 1 |
Pattern observation: The number of operations is constant regardless of the number of enum cases.
Time Complexity: O(1)
This means the time to find the matching case is constant regardless of the number of enum cases.
[X] Wrong: "Switching on enums checks cases one by one, taking time proportional to the number of cases."
[OK] Correct: Swift compiler optimizes simple enum switches to constant time using jump tables or direct dispatch.
Understanding how enum case checks grow helps you explain how your code handles different options efficiently.
"What if we replaced the switch with a dictionary lookup for enum cases? How would the time complexity change?"
Practice
enum in Swift?Solution
Step 1: Understand what enums represent
Enums group related values together to make code clearer and safer.Step 2: Compare options with enum purpose
Only To group related values under one name correctly describes grouping related values under one name.Final Answer:
To group related values under one name -> Option AQuick Check:
Enum purpose = Group related values [OK]
- Confusing enums with functions
- Thinking enums declare variables
- Mixing enums with loops
Direction with cases north, south, east, and west?Solution
Step 1: Recall Swift enum syntax
Enums use the keywordenum, followed by the name and curly braces containingcasedeclarations separated by commas.Step 2: Check each option's syntax
enum Direction { case north, south, east, west } uses correct syntax:enum Direction { case north, south, east, west }. Others use invalid syntax.Final Answer:
enum Direction { case north, south, east, west } -> Option CQuick Check:
Correct enum syntax = enum Direction { case north, south, east, west } [OK]
- Using '=' instead of braces
- Omitting 'case' keyword
- Separating cases with semicolons
enum Light {
case red, yellow, green
}
let current = Light.red
switch current {
case .red:
print("Stop")
case .yellow:
print("Caution")
case .green:
print("Go")
}Solution
Step 1: Identify the enum case assigned
The variablecurrentis assignedLight.red.Step 2: Match the switch case for
The switch matchescurrent.redand prints "Stop".Final Answer:
Stop -> Option DQuick Check:
Light.red triggers "Stop" output [OK]
- Confusing enum cases with strings
- Forgetting dot prefix in switch
- Assuming default output
enum Weather {
case sunny
case rainy
case windy
case
}Solution
Step 1: Review each enum case line
The last line hascasewithout a case name, which is invalid syntax.Step 2: Confirm other parts are correct
Enum name and braces are correct; cases do not need to be strings.Final Answer:
Missing case name after 'case' keyword -> Option AQuick Check:
Every 'case' must have a name [OK]
- Leaving 'case' empty
- Thinking cases need quotes
- Missing braces around enum
Planet with cases for mercury, venus, and earth. Then, write a function that returns a friendly message for each planet. Which code correctly implements this?Solution
Step 1: Check enum declaration syntax
Options A, C, and D declare enum correctly withcasekeyword; B misses it, so B is invalid.Step 2: Check function parameter and switch usage
enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case .mercury: return "Closest to the Sun" case .venus: return "Second planet" case .earth: return "Our home" } } usesswitchwith dot syntax for cases and correct function signature. enum Planet { case mercury, venus, earth } func message(planet: Planet) -> String { if planet == mercury { return "Closest to the Sun" } else if planet == venus { return "Second planet" } else { return "Our home" } } uses if-else without dot syntax (.mercury), causing compile error. enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case mercury: return "Closest to the Sun" case venus: return "Second planet" case earth: return "Our home" } } misses dot prefix in switch cases, causing error.Final Answer:
enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case .mercury: return "Closest to the Sun" case .venus: return "Second planet" case .earth: return "Our home" } } -> Option BQuick Check:
Enum with 'case' and switch with dot cases [OK]
- Omitting 'case' keyword in enum
- Missing dot prefix in switch cases
- Using if without dot prefix for enum cases
Ready to prove it?
Pass the test and this pattern moves unknown
