0
0
Swiftprogramming~10 mins

Associated values per case in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Associated values per case
Define enum with cases
Create enum instance with associated value
Switch on enum instance
Match case and extract associated value
Use associated value in code
This flow shows how to define an enum with associated values, create an instance, switch on it, and extract the associated value for use.
Execution Sample
Swift
enum Result {
    case success(String)
    case failure(Int)
}

let res = Result.success("Done")
switch res {
case .success(let message): print(message)
case .failure(let code): print(code)
}
This code defines an enum with associated values, creates a success case with a message, and prints the message by matching the case.
Execution Table
StepActionEvaluationResult
1Define enum Result with cases success(String) and failure(Int)N/AEnum type created
2Create instance res = Result.success("Done")N/Ares holds success case with "Done"
3Switch on resres is success("Done")Match .success case
4Extract associated value with let messagemessage = "Done"message variable set
5Print messageprint("Done")Output: Done
6Exit switchNo more casesSwitch ends
💡 Switch ends after matching the .success case and printing the associated value
Variable Tracker
VariableStartAfter Step 2After Step 4Final
resundefinedsuccess("Done")success("Done")success("Done")
messageundefinedundefined"Done""Done"
Key Moments - 2 Insights
Why do we use 'let message' inside the case instead of just using '.success'?
Because '.success' is the case name, and 'message' is the associated value inside it. The switch extracts the associated value by naming it with 'let message' as shown in step 4 of the execution_table.
What happens if we try to access the associated value without switching on the enum?
You cannot directly access the associated value without matching the case in a switch or if-case. The enum instance holds the value internally, so you must extract it as shown in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'message' after step 4?
Asuccess("Done")
B"Done"
Cundefined
DInt value
💡 Hint
Check the variable_tracker row for 'message' after step 4
At which step does the switch statement match the .success case?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the execution_table row where the switch evaluates the enum instance
If the enum instance was Result.failure(404), what would be printed?
A"Done"
BNo output
C404
DError
💡 Hint
Think about the failure case and its associated Int value
Concept Snapshot
enum MyEnum {
  case one(Type)
  case two(Type)
}

Create instance: let val = MyEnum.one(value)

Switch and extract:
switch val {
 case .one(let x): use x
 case .two(let y): use y
}

Use associated values to store extra info per case.
Full Transcript
This example shows how to use Swift enums with associated values. First, we define an enum with cases that hold extra data. Then, we create an instance of the enum with one case and its associated value. Next, we use a switch statement to check which case the instance holds. Inside the matching case, we extract the associated value using 'let' and use it, for example, by printing it. The execution table traces each step, showing how variables change and how the switch matches the case. Key moments clarify why we extract values inside the switch and why direct access is not possible. The quiz tests understanding of variable values and switch matching. The snapshot summarizes the syntax and usage for quick reference.