Complete the code to declare an enum with an associated value.
enum Vehicle {
case car([1]: String)
}The enum case car has an associated value named model of type String.
Complete the code to extract the associated value from the enum using a switch statement.
let vehicle = Vehicle.car(model: "Sedan") switch vehicle { case .car(let [1]): print("Model is \(model)") }
let to bind the value.The let model extracts the associated value labeled model from the enum case.
Fix the error in the enum declaration by completing the associated value syntax.
enum Result {
case success([1] Int)
case failure(String)
}The associated value must have a label followed by a colon, like value:.
Fill both blanks to declare an enum case with two associated values.
enum Message {
case text([1]: String, [2]: Int)
}The enum case text has two associated values labeled content and length.
Fill all three blanks to create a dictionary from an enum with associated values filtering by a condition.
let messages = [ Message.text(content: "Hi", length: 2), Message.text(content: "Hello", length: 5), Message.text(content: "Hey", length: 3) ] let filtered = messages.filter { msg in if case .text(let [1], let [2]) = msg { return [2] [3] 3 } return false }
The code extracts content and length from each Message.text case and filters messages with length greater than 3.