Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

Value binding

If we need to access the values that are being tested against, we can bind them to variables within the scope of the switch statement.

Staying with our a, b, and t definitions, we could write the following:

switch t { 
case (let x, 0 ):
print("cannot divide \(x) by b")
case (0, let x ):
print("cannot divide \(x) by a")
default:
print("can divide by both a and b")
}

Although we tested against one value, we are still able to extract the other for use inside the switch.