0
0
Swiftprogramming~20 mins

Associated values per case in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Associated Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of enum with associated values
What is the output of this Swift code?
Swift
enum Result {
    case success(data: String)
    case failure(errorCode: Int)
}

let response = Result.success(data: "File loaded")
switch response {
case .success(let data):
    print("Success: \(data)")
case .failure(let code):
    print("Error code: \(code)")
}
ACompilation error
BError code: 0
CSuccess: data
DSuccess: File loaded
Attempts:
2 left
💡 Hint
Look at which case is assigned to the variable and what the associated value is.
Predict Output
intermediate
2:00remaining
Extracting multiple associated values
What will this Swift code print?
Swift
enum Shape {
    case rectangle(width: Int, height: Int)
    case circle(radius: Int)
}

let shape = Shape.rectangle(width: 5, height: 10)
switch shape {
case .rectangle(let w, let h):
    print("Area: \(w * h)")
case .circle(let r):
    print("Area: \(3.14 * Double(r * r))")
}
AArea: 50
BArea: 15
CArea: 78.5
DRuntime error
Attempts:
2 left
💡 Hint
Calculate the area of the rectangle using width and height.
🔧 Debug
advanced
2:00remaining
Identify the error with associated values
What error does this Swift code produce?
Swift
enum Device {
    case phone(model: String)
    case tablet
}

let myDevice = Device.phone(model: "iPhone 14")
switch myDevice {
case .phone:
    print("Phone detected")
case .tablet:
    print("Tablet detected")
}
ANo error, prints 'Phone detected'
BError: Missing default case in switch
CError: Associated value 'model' not extracted in case pattern
DRuntime crash due to missing associated value
Attempts:
2 left
💡 Hint
Check if the associated value is handled in the switch case pattern.
🧠 Conceptual
advanced
1:30remaining
Understanding associated values in enums
Which statement about Swift enums with associated values is true?
AAll cases must have the same associated value types.
BEach case can store different types and amounts of associated values.
CAssociated values must be constants and cannot be extracted.
DEnums with associated values cannot be used in switch statements.
Attempts:
2 left
💡 Hint
Think about flexibility of enum cases with associated values.
Predict Output
expert
2:30remaining
Output of nested associated values in enum
What is the output of this Swift code?
Swift
enum Response {
    case success(data: (id: Int, message: String))
    case failure(error: String)
}

let result = Response.success(data: (id: 101, message: "OK"))
switch result {
case .success(let data):
    print("ID: \(data.id), Message: \(data.message)")
case .failure(let error):
    print("Error: \(error)")
}
AID: 101, Message: OK
BError: OK
CID: (101, "OK"), Message: nil
DCompilation error due to tuple usage
Attempts:
2 left
💡 Hint
Look at how the tuple is used as an associated value and accessed.