0
0
Swiftprogramming~20 mins

Enum with switch pattern matching in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Enum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of switch with enum and associated values
What is the output of this Swift code using enum and switch pattern matching?
Swift
enum Device {
    case iPhone(model: String)
    case iPad(model: String)
    case mac
}

let myDevice = Device.iPhone(model: "13 Pro")

switch myDevice {
case .iPhone(let model):
    print("iPhone model: \(model)")
case .iPad(let model):
    print("iPad model: \(model)")
case .mac:
    print("Mac computer")
}
AiPhone model: 13 Pro
BMac computer
CiPad model: 13 Pro
DCompilation error
Attempts:
2 left
💡 Hint
Look at the enum case and the associated value passed to it.
Predict Output
intermediate
2:00remaining
Switch pattern matching with multiple associated values
What will this Swift code print when run?
Swift
enum Result {
    case success(code: Int, message: String)
    case failure(error: String)
}

let response = Result.success(code: 200, message: "OK")

switch response {
case .success(let code, let message) where code == 200:
    print("Success with code 200: \(message)")
case .success(let code, let message):
    print("Success with code \(code): \(message)")
case .failure(let error):
    print("Failure: \(error)")
}
ACompilation error
BSuccess with code 200: OK
CFailure: OK
DSuccess with code 200: failure
Attempts:
2 left
💡 Hint
Check the where clause in the first case of the switch.
🔧 Debug
advanced
2:00remaining
Identify the error in switch pattern matching with enum
What error does this Swift code produce when compiled?
Swift
enum Status {
    case ready
    case loading(progress: Int)
    case error(message: String)
}

let currentStatus = Status.loading(progress: 50)

switch currentStatus {
case .ready:
    print("Ready")
case .loading:
    print("Loading")
case .error(let message):
    print("Error: \(message)")
}
ARuntime error: Unexpected nil value
BNo error, prints: Loading
CError: Enum case 'loading' not handled correctly
DError: Missing associated value pattern in case .loading
Attempts:
2 left
💡 Hint
Check how the .loading case is matched in the switch.
🧠 Conceptual
advanced
1:30remaining
Understanding exhaustive switch with enums
Why does Swift require the switch statement to be exhaustive when switching over enums?
ATo force the programmer to use if-else instead of switch
BBecause Swift does not support default cases in switch statements
CTo ensure all possible enum cases are handled, preventing runtime errors
DBecause enums can only have one case
Attempts:
2 left
💡 Hint
Think about safety and completeness in code.
Predict Output
expert
2:30remaining
Output of nested enum with switch pattern matching
What is the output of this Swift code with nested enums and switch pattern matching?
Swift
enum Network {
    enum Response {
        case success(data: String)
        case failure(code: Int)
    }
    case request(url: String, response: Response)
}

let netCall = Network.request(url: "example.com", response: .failure(code: 404))

switch netCall {
case .request(let url, let response):
    switch response {
    case .success(let data):
        print("Success from \(url): \(data)")
    case .failure(let code) where code >= 400:
        print("Error \(code) from \(url)")
    default:
        print("Other response from \(url)")
    }
}
AError 404 from example.com
BSuccess from example.com: failure
COther response from example.com
DCompilation error
Attempts:
2 left
💡 Hint
Look at the nested switch and the where clause condition.