Skip to content

Instantly share code, notes, and snippets.

@fassko
Created May 21, 2018 12:01
Show Gist options
  • Save fassko/6eb8a808120bc83d8b44db4e2cd839f4 to your computer and use it in GitHub Desktop.
Save fassko/6eb8a808120bc83d8b44db4e2cd839f4 to your computer and use it in GitHub Desktop.
Swift switch value bindings
/// Wind speed with direction
enum WindSpeed {
case north(Double)
case east(Double)
case south(Double)
case west(Double)
}
let direction = WindSpeed.north(3.6)
// Switch with value binding before matching pattern
switch direction {
case let .north(speed):
print(speed)
default:
print("default")
}
// Switch with value binding inside matching pattern
switch direction {
case .north(let speed):
print(speed)
default:
print("default")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment