Complete the code to define an enum case with an associated value.
enum Result {
case success([1]: String)
}The enum case success has an associated value named message of type String.
Complete the code to extract the associated value from the enum using a switch statement.
let result = Result.success(message: "Done") switch result { case .success(let [1]): print([1]) default: break }
The associated value message is extracted in the case pattern and printed.
Fix the error in the enum definition with multiple associated values.
enum NetworkResponse {
case error(code: Int, [1]: String)
}The second associated value should be named message to describe the error message.
Fill both blanks to create a function that returns the associated value from the enum.
func getMessage(from response: NetworkResponse) -> String? { switch response { case .error(_, let [1]): return [2] default: return nil } }
code value.The function extracts the message associated value and returns it.
Fill all three blanks to define an enum with multiple cases and associated values, and create an instance.
enum Device {
case phone([1]: String)
case tablet([2]: Int)
case laptop([3]: String)
}
let myDevice = Device.phone(model: "iPhone 14")The enum cases have associated values named model or size describing the device details.