0
0
Swiftprogramming~10 mins

Raw values for enums in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Raw values for enums
Define enum with raw values
Access raw value of case
Use raw value to create enum case
Handle optional enum from raw value
End
This flow shows how to define an enum with raw values, access those values, create enum cases from raw values, and handle optional results.
Execution Sample
Swift
enum Direction: String {
    case north = "N"
    case south = "S"
    case east = "E"
    case west = "W"
}

let dir = Direction.north
print(dir.rawValue)

let fromRaw = Direction(rawValue: "E")
print(fromRaw)
Defines an enum Direction with String raw values, prints the raw value of north, and creates an enum case from a raw value.
Execution Table
StepActionEvaluationResult
1Define enum Direction with raw valuesN/AEnum Direction created with cases north="N", south="S", east="E", west="W"
2Assign dir = Direction.northdir = Direction.northdir holds case north
3Print dir.rawValuedir.rawValue"N"
4Create fromRaw = Direction(rawValue: "E")Direction(rawValue: "E")Optional(Direction.east)
5Print fromRawfromRawOptional(Direction.east)
💡 All steps complete; raw values accessed and enum created from raw value successfully.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
dirnilDirection.northDirection.northDirection.north
fromRawnilnilOptional(Direction.east)Optional(Direction.east)
Key Moments - 3 Insights
Why is fromRaw an Optional?
Because Direction(rawValue:) initializer returns an Optional in case the raw value does not match any case, as shown in step 4 of the execution_table.
Can raw values be of any type?
No, raw values must be of a literal type like String, Int, or Float, as shown by the String raw values in the enum definition in step 1.
Does accessing rawValue change the enum case?
No, accessing rawValue only reads the associated raw value without changing the enum case, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of dir.rawValue at step 3?
A"E"
B"N"
COptional(Direction.north)
Dnil
💡 Hint
Refer to step 3 in execution_table where dir.rawValue is printed.
At which step does fromRaw get assigned an Optional enum case?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check step 4 in execution_table where fromRaw is created from rawValue.
If you try Direction(rawValue: "X"), what would fromRaw be?
AOptional(Direction.north)
BOptional(Direction.east)
Cnil
DCrash
💡 Hint
Recall from key_moments that rawValue initializer returns Optional and fails safely if no match.
Concept Snapshot
enum Direction: String { case north = "N", south = "S" }
Access raw value: dir.rawValue
Create from raw: Direction(rawValue: "N") -> Optional
Raw values must be literal types
Raw value init returns Optional if no match
Full Transcript
This example shows how to define an enum with raw values in Swift. The enum Direction has cases with String raw values like "N" for north. We assign dir to Direction.north and print its rawValue, which outputs "N". Then we create fromRaw using Direction(rawValue: "E"), which returns an Optional containing Direction.east. The rawValue initializer returns Optional because the raw value might not match any case. Raw values must be literal types such as String or Int. Accessing rawValue does not change the enum case. This helps safely convert between raw values and enum cases.