As we saw in the Enumerating values with enums recipe from Chapter 1, Swift Building Blocks, enums can have associated values, and we can use an if statement to both check an enum's case and extract the associated value in one expression.
Let's create an enum to represent the result of the pool game, with each case having an associated message:
enum FrameResult {
case win(congratulations: String)
case lose(commiserations: String)
}
Next, we'll create a function that takes a Result and prints either the congratulatory message or the commiseration message:
func printMessage(forResult result: FrameResult) {
if case Result.win(congratulations: let winMessage) = result {
print("You won! \(winMessage)")
} else if case Result.lose(commiserations: let loseMessage) =
result {
print("You lost :( \(loseMessage)")
}
}
Calling this function will print the result, followed by the...