0
0
Swiftprogramming~5 mins

Raw values for enums in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AArray
BString
CDictionary
DClass
How do you declare an enum with Int raw values starting from 1?
Aenum Number: String { case one = "1", two, three }
Benum Number { case one = 1, two, three }
Cenum Number: Int { case one = 1, two, three }
Denum Number: Int { case one, two, three }
What does Direction(rawValue: "N") return if Direction enum has a case with raw value "N"?
AThe enum case matching "N"
Bnil
CA string "N"
DAn error
What is the type of Direction.north.rawValue if Direction is declared as enum Direction: String?
AString
BDirection
CInt
DOptional String
Can you assign different raw value types to different cases in the same enum?
AOnly if the enum is generic
BYes, each case can have its own type
COnly if you use associated values
DNo, all cases must have the same raw value type
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.