分解成個體變數
可以使用以下語法將元組分解為單個變數:
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"