0
0
Swiftprogramming~10 mins

Raw values for enums in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"North"
BNorth
C'North'
Dnorth
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted identifiers as raw values.
Using single quotes instead of double quotes for strings.
2fill in blank
medium

Complete 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'
A"1"
B1
C0
Dmonday
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "1" instead of integer 1.
Starting raw values at 0 when 1 is required.
3fill in blank
hard

Fix 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'
Ared
BRed
C'Red'
D"Red"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes.
Using unquoted identifiers as raw values.
4fill in blank
hard

Fill 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'
A3
B2
C4
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of integers.
Assigning the same raw value to both cases.
5fill in blank
hard

Fill 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'
A"Success"
B"Failure"
C"Unknown"
D"Error"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings or single quotes.
Assigning incorrect raw values that don't match the case.