Tuples in Swift, Advanced Usage and Best Practices

Tuple Destructuring

released Fri, 01 Mar 2019
Swift Version 5.0

Tuple Destructuring

Swift took a lot of inspiration from different programming languages, and this is something that Python has been doing for years. While the previous examples mostly showed how to easily get something into a tuple, destructuring is a swifty way of getting something out of a tuple, and in line with the abc example above, it looks like this:

func abc() -> (Int, Int, Int) {

   return (1, 2, 3)

}

let (a, b, c) = abc()

print(a) // prints 1

Another example is getting several function calls into one line:

let (a, b, c) = (a(), b(), c())

Or, an easy way to swap two values:

var v1: Int

var v2: Int

(v1, v2) = (5, 4)

(a: v1, b: v2) = (a: v2, b: v1) // swapped: v1 == 4, v2 == 5

(v1, v2) = (5, 4)

(a: v1, b: v2) = (b: v1, a: v2) // swapped: v1 == 4, v2 == 5