0
0
Swiftprogramming~10 mins

Enum with switch pattern matching in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum with switch pattern matching
Define Enum with cases
Create Enum instance
Switch on Enum instance
Match case 1?
NoMatch case 2?
Execute case 1
End switch
We define an enum, create an instance, then use a switch to check which case it matches and run code accordingly.
Execution Sample
Swift
enum Direction {
    case north
    case south
    case east
    case west
}

let dir = Direction.east

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 directions, sets one, then prints a message based on the direction using switch.
Execution Table
StepActionEnum InstanceSwitch Case CheckedMatch?Output
1Create enum instanceDirection.east---
2Switch startDirection.eastcase .northNo-
3Switch continueDirection.eastcase .southNo-
4Switch continueDirection.eastcase .eastYesGoing east
5Switch endsDirection.east---
💡 Matched case .east, switch ends after executing its block.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
dirundefinedDirection.eastDirection.eastDirection.eastDirection.eastDirection.east
Key Moments - 3 Insights
Why does the switch check each case one by one?
The switch tries cases in order until it finds a match, as shown in rows 2-4 of the execution table.
What happens if no case matches?
If no case matches, the switch would run a default case if provided, but here it matches at step 4 and stops.
Why do we write cases as .north instead of Direction.north inside switch?
Inside switch on an enum instance, Swift lets us use shorthand .caseName to avoid repetition, as seen in the code sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the enum instance value at step 3?
ADirection.north
BDirection.east
CDirection.south
DDirection.west
💡 Hint
Check the 'Enum Instance' column at step 3 in the execution table.
At which step does the switch find a matching case?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Match?' column to see where it says 'Yes'.
If the enum instance was Direction.west, which case would match?
Acase .west
Bcase .south
Ccase .north
Dcase .east
💡 Hint
Refer to the enum cases and how switch matches the instance value.
Concept Snapshot
enum Direction { case north, south, east, west }
let dir = Direction.east
switch dir {
 case .north: ...
 case .south: ...
 case .east: ...
 case .west: ...
}
Use switch to run code based on enum case, matching one case exactly.
Full Transcript
We start by defining an enum called Direction with four cases: north, south, east, and west. Then we create a variable dir and set it to Direction.east. Next, we use a switch statement on dir. The switch checks each case in order: first .north, then .south, then .east. When it reaches .east, it matches and prints 'Going east'. The switch then ends. If no case matched, a default case would run if provided. Inside the switch, we use shorthand .caseName instead of full enum names. This example shows how to use enums with switch pattern matching in Swift.