Tuples are a data type introduced in Swift version 4. They are used to group multiple values, separated by commas ,, into a single value that is enclosed in parentheses ().

Note: Tuples are compound types. This means that they combine different types of data.


var myTuple = (value1, value2, ...)

Note: Names should be in camelCase.

Accessing and Changing Values

Values of a tuple can be accessed using indices:

var computerScience = (1960, "George Forsythe")
// Output: 1960
// Output: George Forsythe

Values or elements can also be referenced with a named property:

var alanTuring = (
name: "Alan Mathison Turing",
born: 1912,
inventions: ["Universal Turing Machine", "Bombe"]
print("\(alanTuring.name) was born in \(alanTuring.born) and invented the \(alanTuring.inventions[0]).")
// Alan Mathison Turing was born in 1912 and invented the Universal Turing Machine.

These values can then be altered through their indices or name:

computerScience.0 = 1961
// Output: 1961
alanTuring.inventions.append("Automatic Computing Engine")
// Output: ["Universal Turing Machine", "Bombe", "Automatic Computing Engine"]
Edit this page on GitHub


Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Swift on Codecademy