Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an enum with raw values of type String.
Swift
enum Direction: String {
case north = [1]
case south = "South"
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted identifiers as raw values.
Using single quotes instead of double quotes for strings.
✗ Incorrect
The raw value for the case must be a String literal, so it needs to be in double quotes.
2fill in blank
mediumComplete the code to declare an enum with raw values of type Int starting at 1.
Swift
enum Weekday: Int {
case monday = [1]
case tuesday
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "1" instead of integer 1.
Starting raw values at 0 when 1 is required.
✗ Incorrect
Raw values of type Int must be integers. To start at 1, assign monday = 1.
3fill in blank
hardFix the error in the enum raw value declaration.
Swift
enum Color: String {
case red = [1]
case blue = "Blue"
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes.
Using unquoted identifiers as raw values.
✗ Incorrect
Raw values of type String must be enclosed in double quotes. Using "Red" is correct.
4fill in blank
hardFill both blanks to create an enum with raw values and access a raw value.
Swift
enum Planet: Int {
case earth = [1]
case mars = [2]
}
let marsValue = Planet.mars.rawValue Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of integers.
Assigning the same raw value to both cases.
✗ Incorrect
Assign earth = 2 and mars = 3 to set raw values correctly.
5fill in blank
hardFill all three blanks to create an enum with raw values and a function returning a raw value.
Swift
enum Status: String {
case success = [1]
case failure = [2]
case unknown = [3]
}
func getStatusMessage(_ status: Status) -> String {
return status.rawValue
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings or single quotes.
Assigning incorrect raw values that don't match the case.
✗ Incorrect
Each case must have a string raw value in double quotes matching the case meaning.