func getPoint() -> (Int, Int) {
return (5, 5)
}
var str: String
let point = getPoint()
switch (point) {
case (0, 0):
str = "(0, 0) point"
case (_, 1):
str = "(\(point.0), 1) point"
case (1, let y):
str = "(1, \(y)) point"
case (let x, let y) where x == y:
str = "(\(x), \(y)) point"
default:
str = "other point"
}
//str is (5, 5) point
print("str is \(str)")
| You can use tuples to test multiple values in the same switch statement. Each element of the tuple can be tested against a different value or interval of values. Alternatively, use the underscore character (_), also known as the wildcard pattern, to match any possible value. A switch case can use a where clause to check for additional conditions. The switch case matches the current value of point only if the where clause’s condition evaluates to true for that value. |