Recall & Review
beginner
What is a raw value in a Swift enum?
A raw value is a constant value of a specific type (like String or Int) that each enum case can have. It lets you assign a fixed value to each case.
Click to reveal answer
beginner
How do you declare an enum with raw values in Swift?
You add a type after the enum name, like
enum Direction: String, then assign raw values to each case, e.g., case north = "N".Click to reveal answer
intermediate
Can raw values be of any type in Swift enums?
No, raw values must be of a literal type like String, Int, or Character. You cannot use complex types like arrays or dictionaries as raw values.
Click to reveal answer
beginner
How do you access the raw value of an enum case?
Use the
.rawValue property on the enum case. For example, Direction.north.rawValue returns the raw value assigned to north.Click to reveal answer
intermediate
How can you create an enum case from a raw value?
Use the initializer
EnumType(rawValue: value). It returns an optional enum case matching the raw value or nil if no match is found.Click to reveal answer
Which of these is a valid raw value type for a Swift enum?
✗ Incorrect
Raw values must be literal types like String, Int, or Character. Arrays, dictionaries, and classes are not allowed.
How do you declare an enum with Int raw values starting from 1?
✗ Incorrect
You specify the raw value type after the enum name and assign the first case a raw value. The rest auto-increment.
What does
Direction(rawValue: "N") return if Direction enum has a case with raw value "N"?✗ Incorrect
The initializer returns the enum case that has the raw value "N" or nil if none matches.
What is the type of
Direction.north.rawValue if Direction is declared as enum Direction: String?✗ Incorrect
The rawValue property returns the raw value type, which is String in this case.
Can you assign different raw value types to different cases in the same enum?
✗ Incorrect
All enum cases must share the same raw value type declared for the enum.
Explain what raw values are in Swift enums and how to use them.
Think about fixed values assigned to enum cases.
You got /4 concepts.
Describe the difference between raw values and associated values in Swift enums.
Consider how each enum case stores data.
You got /4 concepts.