Swift Tuple Type

Learn how to use the Swift tuple data type to store multiple values within a single entity.

Introducing the Swift tuple

The tuple is perhaps one of the simplest but, most powerful features of the Swift programming language. Quite simple, a tuple is, quite simply, a way to temporarily group together multiple values into a single entity. The items stored in a tuple can be of any type and there are no restrictions requiring that those values all be of the same type. A tuple could, for example, be constructed to contain an Int value, a Double value, and a String as follows:

let myTuple = (10, 432.433, "This is a String")

The elements of a tuple can be accessed using a number of different techniques. A specific tuple value may be accessed simply by referencing the index position (with the first value being at index position 0). The code below, for example, extracts the string resource (at index position 2 in the tuple), and assigns it to a new string variable:

Get hands-on with 1400+ tech skills courses.