The Swift Programming Language (Swift 4.1): Patterns
original source : https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html
Value-Binding Pattern
A value-binding pattern binds matched values to variable or constant names. Value-binding patterns that bind a matched value to the name of a constant begin with the let
keyword; those that bind to the name of variable begin with the var
keyword.
Identifiers patterns within a value-binding pattern bind new named variables or constants to their matching values. For example, you can decompose the elements of a tuple and bind the value of each element to a corresponding identifier pattern.
let point = (3, 2)
switch point {
// Bind x and y to the elements of point.
case let (x, y):
print("The point is at ((x), (y)).")
}
// Prints "The point is at (3, 2)."
In the example above, let
distributes to each identifier pattern in the tuple pattern (x, y)
. Because of this behavior, the switch
cases case let (x, y):
and case (let x, let y):
match the same values.