Swift Language Tuples Decomposing into individual variables

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Tuples can be decomposed into individual variables with the following syntax:

let myTuple = (name: "Some Name", age: 26)
let (first, second) = myTuple

print(first) // "Some Name"
print(second)  // 26

This syntax can be used regardless of if the tuple has unnamed properties:

let unnamedTuple = ("uno", "dos")
let (one, two) = unnamedTuple
print(one) // "uno"
print(two) // "dos"

Specific properties can be ignored by using underscore (_):

let longTuple = ("ichi", "ni", "san")
let (_, _, third) = longTuple
print(third) // "san"


Got any Swift Language Question?