0
0
Swiftprogramming~10 mins

Enum methods and computed properties in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum methods and computed properties
Define Enum with cases
Add computed property
Add method
Create enum instance
Access computed property
Call method
Use returned values
This flow shows how to define an enum with computed properties and methods, then create instances and use those features.
Execution Sample
Swift
enum Direction {
    case north, south, east, west

    var description: String {
        switch self {
        case .north: return "Up"
        case .south: return "Down"
        case .east: return "Right"
        case .west: return "Left"
        }
    }

    func opposite() -> Direction {
        switch self {
        case .north: return .south
        case .south: return .north
        case .east: return .west
        case .west: return .east
        }
    }
}

let dir = Direction.north
print(dir.description)
print(dir.opposite())
Defines an enum Direction with a computed property description and a method opposite, then prints values for north.
Execution Table
StepActionEnum InstanceComputed Property AccessMethod CallOutput
1Create enum instancenorth---
2Access computed property 'description'northUp-Up
3Call method 'opposite()'north-southsouth
4Print description---Up
5Print opposite---south
6End of program----
💡 Program ends after printing description and opposite direction.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
dirnilnorthnorthnorthnorth
Key Moments - 3 Insights
Why does accessing 'description' not require parentheses?
Because 'description' is a computed property, not a method, so you access it like a variable (see execution_table step 2).
Why do we use 'self' inside the computed property and method?
'self' refers to the current enum instance, allowing the switch to choose the right case (see code in execution_sample).
What is returned by the method 'opposite()' when called on 'north'?
It returns the enum case 'south', the opposite direction (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when accessing 'description' for 'dir' at step 2?
A"north"
B"Up"
C"Down"
D"south"
💡 Hint
Check the 'Computed Property Access' and 'Output' columns at step 2 in the execution_table.
At which step does the method 'opposite()' return 'south'?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Method Call' column in execution_table for the returned value.
If we change 'dir' to '.east', what would 'dir.opposite()' return?
A.south
B.north
C.west
D.east
💡 Hint
Refer to the method 'opposite()' switch cases in the code sample.
Concept Snapshot
enum Direction {
  case north, south, east, west

  var description: String { // computed property
    switch self {
    case .north: return "Up"
    ...
    }
  }

  func opposite() -> Direction { // method
    switch self {
    case .north: return .south
    ...
    }
  }
}

Use computed properties like variables, methods with parentheses.
Full Transcript
This example shows how to add computed properties and methods to a Swift enum. We define an enum Direction with four cases. The computed property 'description' returns a string for each case using a switch on self. The method 'opposite()' returns the opposite direction. We create an instance 'dir' set to north. Accessing 'dir.description' returns "Up". Calling 'dir.opposite()' returns the enum case south. Computed properties are accessed without parentheses, methods require parentheses. The 'self' keyword refers to the current enum case inside the property and method. This helps organize related data and behavior inside enums clearly.