What if you could make your code smarter by teaching your enums to think for themselves?
Why Enum methods and computed properties in Swift? - Purpose & Use Cases
Imagine you have a list of different types of fruits, and for each fruit, you want to know its color and whether it's tropical. You try to write separate code everywhere to check these details manually.
This manual way means repeating the same checks in many places. It's easy to forget or make mistakes, and if you add a new fruit, you have to update all those checks again. It becomes slow and confusing.
Using enum methods and computed properties lets you keep all related information and behavior about each fruit inside one place. You write the rules once, and the program automatically knows the color or tropical status whenever you ask.
if fruit == "apple" { color = "red" } else if fruit == "banana" { color = "yellow" } // repeated in many places
enum Fruit {
case apple, banana
var color: String {
switch self {
case .apple: return "red"
case .banana: return "yellow"
}
}
}This makes your code cleaner, easier to update, and smarter by bundling data and behavior together.
Think of a weather app that uses enums for different weather types, each with methods to show icons or suggest clothing, all neatly organized and easy to maintain.
Manual checks are repetitive and error-prone.
Enum methods and computed properties keep related info together.
They make code easier to read, update, and use.
Practice
Solution
Step 1: Understand enum methods
Enum methods allow you to add functions that perform tasks related to the enum's cases.Step 2: Differentiate from other enum features
Enums do not store extra data like classes; methods help organize related actions.Final Answer:
To perform actions or calculations related to enum cases -> Option CQuick Check:
Enum methods = perform actions [OK]
- Thinking enums can inherit from other enums
- Confusing methods with stored properties
- Assuming enums convert to classes automatically
Solution
Step 1: Identify computed property syntax
Computed properties use var with a code block returning a value, likevar name: Type { ... }.Step 2: Check options
var description: String { return "Example" } matches computed property syntax; B is a method, C is a stored property, D is a static stored property.Final Answer:
var description: String { return "Example" } -> Option DQuick Check:
Computed property syntax = var with braces [OK]
- Using func instead of var for computed properties
- Declaring computed properties as let constants
- Confusing static stored properties with computed ones
enum Direction {
case north, south, east, west
var isVertical: Bool {
return self == .north || self == .south
}
func description() -> String {
switch self {
case .north: return "Up"
case .south: return "Down"
case .east: return "Right"
case .west: return "Left"
}
}
}
let dir = Direction.east
print(dir.isVertical)
print(dir.description())What is the output?
Solution
Step 1: Evaluate computed property isVertical
For Direction.east, isVertical checks if east == north or south, which is false.Step 2: Evaluate description() method
Switch on east returns "Right" string.Final Answer:
false Right -> Option AQuick Check:
east isVertical = false, description = Right [OK]
- Assuming east is vertical
- Confusing method return values with case names
- Forgetting to call method with parentheses
enum Status {
case success, failure
var message: String {
switch self {
case .success:
return "Success"
case .failure
return "Failure"
}
}
}What is the error and how to fix it?
Solution
Step 1: Identify syntax error in switch cases
Case .failure is missing a colon ':' after it, causing a syntax error.Step 2: Fix the syntax
Add ':' after case .failure to correct the switch syntax.Final Answer:
Missing colon after case .failure; add ':' after .failure -> Option BQuick Check:
Switch case syntax requires ':' [OK]
- Thinking computed properties need func keyword
- Adding raw values unnecessarily
- Removing return statements inside computed properties
Light with cases red, yellow, and green. Add a computed property time that returns the duration in seconds for each light: red = 30, yellow = 5, green = 25. Also add a method next() that returns the next light in the traffic cycle (red -> green -> yellow -> red). Which implementation is correct?Solution
Step 1: Check computed property time values
enum Light { case red, yellow, green var time: Int { switch self { case .red: return 30 case .yellow: return 5 case .green: return 25 } } func next() -> Light { switch self { case .red: return .green case .green: return .yellow case .yellow: return .red } } } correctly returns 30 for red, 5 for yellow, and 25 for green as required.Step 2: Check next() method cycle order
enum Light { case red, yellow, green var time: Int { switch self { case .red: return 30 case .yellow: return 5 case .green: return 25 } } func next() -> Light { switch self { case .red: return .green case .green: return .yellow case .yellow: return .red } } } cycles red -> green -> yellow -> red, matching the problem statement.Final Answer:
enum Light { case red, yellow, green var time: Int { switch self { case .red: return 30 case .yellow: return 5 case .green: return 25 } } func next() -> Light { switch self { case .red: return .green case .green: return .yellow case .yellow: return .red } } } -> Option AQuick Check:
Correct time values and cycle order = enum Light { case red, yellow, green var time: Int { switch self { case .red: return 30 case .yellow: return 5 case .green: return 25 } } func next() -> Light { switch self { case .red: return .green case .green: return .yellow case .yellow: return .red } } } [OK]
- Mixing up time values for colors
- Incorrect next() cycle order
- Using func instead of computed property for time
Ready to prove it?
Pass the test and this pattern moves unknown
