Switching on multiple values
Switch statements are so incredibly powerful in Swift — especially when used with multiple values and pattern matching. Since cases are evaluated from top to bottom, by putting more specific cases on top, we can create a chain of rules to be evaluated.
func player(_ player: Player,
didCollideWith object: GameObject) {
switch (player.state, object.kind) {
case (.invincible, _):
object.destroy()
case (.damaged, .enemy):
gameOver()
case (_, .enemy):
player.takeDamage()
case (_, .obstacle):
player.stopMoving()
}
}