分解成个体变量
可以使用以下语法将元组分解为单个变量:
let myTuple = (name: "Some Name", age: 26)
let (first, second) = myTuple
print(first) // "Some Name"
print(second) // 26
无论元组是否具有未命名的属性,都可以使用此语法:
let unnamedTuple = ("uno", "dos")
let (one, two) = unnamedTuple
print(one) // "uno"
print(two) // "dos"
使用下划线(_
)可以忽略特定属性:
let longTuple = ("ichi", "ni", "san")
let (_, _, third) = longTuple
print(third) // "san"