Swift version: 3.0 | View all code snippets in this post on Github

Declaring Tuples

Tuples group multiple values of possibly different types. They are declared using parentheses:

let slowTextSpeed = (1, "Slow")

The type can be explicitly set like so:

let fastTextSpeed: (Int, String) = (3, "Fast")

You can also name the values for easier access:

var textSpeed = (value: 2, label: "Medium")

Getting Tuple Values

Use dot syntax to access the value stored at a given index:

var slowSpeedLabel = slowTextSpeed.1
// -> Slow

To retrieve some values while ignoring others, use the following syntax:

let (_, fastSpeedLabel) = fastTextSpeed
fastSpeedLabel
// -> Fast
let (fastSpeedValue, _) = fastTextSpeed
fastSpeedValue
// -> 3

If the values were labeld during declaration, you can access them using their labels:

let speedLabel = textSpeed.label
// -> Medium

Updating Tuple Values

Declare tuples as variables in order to be able to update their values, then use dot syntax (with indices or labels) to do so:

textSpeed.label = "Fast"
textSpeed.value = 3
// -> (3, "Fast")