Challenge - 5 Problems
Swift Enum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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") }
Attempts:
2 left
💡 Hint
Look at the enum case and the associated value passed to it.
✗ Incorrect
The variable myDevice is assigned the enum case iPhone with model "13 Pro". The switch matches .iPhone and prints the model.
❓ Predict Output
intermediate2: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)") }
Attempts:
2 left
💡 Hint
Check the where clause in the first case of the switch.
✗ Incorrect
The response matches the first case because code is 200, so it prints the message with that case.
🔧 Debug
advanced2: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)") }
Attempts:
2 left
💡 Hint
Check how the .loading case is matched in the switch.
✗ Incorrect
The .loading case has an associated value but the switch case does not bind or ignore it, causing a compile error.
🧠 Conceptual
advanced1:30remaining
Understanding exhaustive switch with enums
Why does Swift require the switch statement to be exhaustive when switching over enums?
Attempts:
2 left
💡 Hint
Think about safety and completeness in code.
✗ Incorrect
Swift requires exhaustive switches to make sure all enum cases are handled, avoiding unexpected behavior at runtime.
❓ Predict Output
expert2: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)") } }
Attempts:
2 left
💡 Hint
Look at the nested switch and the where clause condition.
✗ Incorrect
The outer switch matches .request and extracts url and response. The inner switch matches .failure with code 404 which is >= 400, so it prints the error message.