Value Binding and where Clauses
Bind matched values and add extra conditions.
Binding in a Case
A switch case can bind the matched value to a constant with case let x, making the value usable inside that case.
let value = 42
switch value {
case let n:
print("The number is \(n)")
}Binding Part of a Tuple
You can bind just the parts of a tuple you need, using let on specific positions.
let point = (3, 7)
switch point {
case (let x, 0):
print("On x-axis at \(x)")
case (0, let y):
print("On y-axis at \(y)")
case let (x, y):
print("At \(x), \(y)")
}All lessons in this course
- Exhaustive Switch on Values
- Matching Ranges and Tuples
- Value Binding and where Clauses
- Compound Cases and Fallthrough