0
0
Swiftprogramming~10 mins

Enum declaration and cases in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum declaration and cases
Start
Declare enum with cases
Create variable of enum type
Assign one of the enum cases
Use enum variable in code
End
This flow shows how to declare an enum, create a variable of that enum type, assign a case, and then use it.
Execution Sample
Swift
enum Direction {
    case north
    case south
    case east
    case west
}

var currentDirection = Direction.north
Declares an enum Direction with four cases and assigns the north case to a variable.
Execution Table
StepActionCode LineVariableValueNotes
1Declare enum Direction with cases north, south, east, westenum Direction {...}--Enum type created
2Create variable currentDirection of type Directionvar currentDirection = Direction.northcurrentDirectionDirection.northVariable assigned enum case north
3Use currentDirection in code (example: print)print(currentDirection)currentDirectionnorthOutputs 'north' when printed
💡 All enum cases declared and variable assigned successfully
Variable Tracker
VariableStartAfter Step 2After Step 3
currentDirectionundefinedDirection.northDirection.north
Key Moments - 2 Insights
Why do we write Direction.north instead of just north when assigning?
Because north is a case inside the Direction enum, we must specify the enum type to avoid confusion. See execution_table step 2 where currentDirection is assigned Direction.north.
Can we assign a value to an enum case directly?
No, enum cases are fixed identifiers. You assign the case itself, not a separate value. This is shown in execution_table step 2 where currentDirection gets assigned the case Direction.north.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of currentDirection after step 2?
ADirection.south
Bundefined
CDirection.north
Dnorth
💡 Hint
Check the 'Value' column in execution_table row for step 2.
At which step is the enum Direction declared?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for the declaration.
If we change currentDirection to Direction.west at step 2, what will be printed at step 3?
ADirection.west
Bwest
Cnorth
Dundefined
💡 Hint
The variable value changes at step 2 and is printed at step 3, see variable_tracker.
Concept Snapshot
enum EnumName {
  case case1
  case case2
  ...
}

Use with: var v = EnumName.case1
Enum cases are fixed named values grouped under one type.
Full Transcript
This example shows how to declare an enum named Direction with four cases: north, south, east, and west. Then, a variable currentDirection is created and assigned the case Direction.north. The execution table traces the steps: first the enum is declared, then the variable is assigned, and finally the variable is used (for example, printed). The variable tracker shows currentDirection starts undefined and becomes Direction.north after assignment. Key moments clarify why we use Direction.north instead of just north and that enum cases are fixed identifiers. The quiz questions test understanding of variable values at each step and the enum declaration step.