Tuples

Sometimes data comes in pairs or triplets. An example of this is a pair of (x, y) coordinates on a 2D grid. Similarly, a set of coordinates on a 3D grid is comprised of an x-value, a y-value and a z-value.

In Swift, you can represent such related data in a very simple way through the use of a tuple.

A tuple is a type that represents data composed of more than one value of any type. You can have as many values in your tuple as you like. For example, you can define a pair of 2D coordinates where each axis value is an integer, like so:

let coordinates: (Int, Int) = (2, 3)

The type of coordinates is a tuple containing two Int values. The types of the values within the tuple, in this case Int, are separated by commas surrounded by parentheses. The code for creating the tuple is much the same, with each value separated by commas and surrounded by parentheses.

Type inference can infer tuple types too:

let coordinates = (2, 3)

You could similarly create a tuple of Double values, like so:

let coordinates = (2.1, 3.5)

// Inferred to be of type (Double, Double)

Or you could mix and match the types comprising the tuple, like so:

let coordinates = (2.1, 3)

// Inferred to be of type (Double, Int)

And here’s how to access the data inside a tuple:

let coordinates = (2, 3) let x = coordinates.0 let y = coordinates.1

You can reference each item in the tuple by its position in the tuple, starting with zero. So in this example, x will equal 2 and y will equal 3.

Note: Starting with zero is a common practice in computer programming and is called zero indexing. You’ll see this again in Chapter 10, “Arrays”.

In the previous example, it may not be immediately obvious that the first value, at index 0, is the x-coordinate and the second value, at index 1, is the y-coordinate. This is another demonstration of why it’s important to always name your variables in a way that avoids confusion.

Fortunately, Swift allows you to name the individual parts of a tuple and you can be explicit about what each part represents. For example:

let coordinatesNamed = (x: 2, y: 3)

// Inferred to be of type (x: Int, y: Int)

Here, the code annotates the values of coordinatesNamed to contain a label for each part of the tuple.

Then, when you need to access each part of the tuple, you can access it by its name:

let x = coordinatesNamed.x let y = coordinatesNamed.y

This is much clearer and easier to understand. More often than not, it’s helpful to name the components of your tuples.

If you want to access multiple parts of the tuple at the same time, as in the examples above, you can also use a shorthand syntax to make it easier:

let coordinates3D = (x: 2, y: 3, z: 1) let (x, y, z) = coordinates3D

This declares three new constants, x, y and z, and assigns each part of the tuple to them in turn. The code is equivalent to the following:

let coordinates3D = (x: 2, y: 3, z: 1) let x = coordinates3D.x

let y = coordinates3D.y let z = coordinates3D.z

If you want to ignore a certain element of the tuple, you can replace the

corresponding part of the declaration with an underscore. For example, if you were performing a 2D calculation and wanted to ignore the z-coordinate of coordinates3D, then you’d write the following:

let (x, y, _) = coordinates3D

This line of code only declares x and y. The _ is special and simply means you’re ignoring this part for now.

Note: You’ll find that you can use the underscore throughout Swift to ignore a value.

results matching ""

    No results matching ""