Swift tuple: The definition and how you can use it in practical iOS development

Swift is a programming language for iOS app development. It is a product of Apple. So in order to continue the series of articles about Swift, in this blog, ArrowHiTech will help you to know more about Tuple swift – The definition and how you can use it in practical iOS development. Let’s discover. 

What’s tuple?

A tuple (pronounced “TEW-ple”) is a list of values. Its Tuple swift values are ordered and separated by commas and surrounded in parentheses.

For example: 

Swift tuple

In this example, the Tuple swift is (“LAX”, 747). Two values are clearly identified. Because it is separated by a comma and enclosed in parenthesis. Using the commas and parentheses is the easiest way to distinguish a tuple in everyday coding.

The type of the Tuple swift is (String, Int). The first value’s type is String. While the second value’s type is Int. A comma is used to separate the values. And the parenthesis is surrounded.

Furthermore, the Tuple swift is a list. So we can access its components by using the indices 0, 1, and so on. In the next part in this blog, we will show more ways to deal with values. Additionally, there are many kinds of types in one Tuple value. You also can change its value if the tuple marked mutable with var.  However, after it runs, you can not add any things to it. 

Additionally, the order of a Tuple swift cannot be changed when it has been created. As a result, the example below are different: 

Swift tuple

It’s important to keep in the right order. The example above has the same value with the same type. However, their types differ according to the order of the values. Additionally, if the types of the tuples are the same and the types themselves follow the Equatable protocol, you can compare them. Or if you can compare their values, you can compare their tuples as well.

Below are example:

Swift tuple

In the 2 first lines of the code, we’re comparing tuples  which have the same type. Because the individual types are Equatable, we can use == to determine equality. 

How to use Tuple swift?

Tuple swift are highly lightweight data structures . Because Tuple swift allows you to create, store, and pass around groupings of values of various types. When trying to return many values from a function in Swift as a single compound value, this can be quite helpful.

Creating Tuple swift

How to create a basic Tuple swift consisting of a integer and a string:

Swift tuple

Because the compiler will figure it out for you, Tuple swift’s type definition can be ignored:

Swift tuple

Individual elements can also be named to define a Tuple swift:

Swift tuple

Reading values from tuple

Decompose a Tuple swift into individual variables or constants as follows:

Swift tuple

With an underscore, you can ignore sections of the tuple:

Swift tuple

Individual elements of the tuple can access using indexes:

Swift tuple

You may also use element names if you defined the tuple by naming specific elements:

Swift tuple

Using Tuple swift as return values of functions

Tuples become useful. Because it tries to return numerous values from a function. So let’s consider the following example function. It combines two values (a String and an Int) and returns a tuple with named elements:

iOS programming language

The following is an example of how to read the return value of the getCity() function:

iOS app development

How to destructuring Tuple swift

You don’t have to refer to the values of a tuple by their indices. Instead, you can use variable names! Let’s take a look at the following example:

  1. let flight = (airport: “LAX”, airplane: 747, heading: 270)
  2. print(“We’re flying a \(flight.airplane) towards \(flight.airport) with heading \(flight.heading) degrees.”)

To access one of the values from flight, we use syntax like flight.airport. Moreover, the tuple includes three names and three values, separated by colons and commas. Thus, this syntax is the same with how you access the properties of an object. 

Besides that,you can “decompose” the tuple into individual variables or constants as the following:

app development iOS

Interpret the example above: 

  • The Tuple swift consists of (“LAX”, 747, 270). Its values are divided into independent constants, airport and heading, rather than being assigned as a whole to a variable or constant.
  • The airport constant with the value “LAX” is being printed. This value comes from the Tuple swift. But because of the deconstruction, it’s now a separate constant.
  • We’re ignoring the underscore “_” as a value. This is standard Swift syntax, such as in for loops with for in 1…5.

Consider the case where we’re running a function that returns an HTTP status code. Because a code usually includes a number, such as 404, and some text, such as Not Found. Assume the function is part of a framework. And we are unable to modify its code.


We’re deconstructing the Tuple swift to get at the individual values (code, text). The values can then be printed using the individual constants code and text.

4. Further Reading

Tuple swift is a basic concept that has a lot of power. It is surprise that a few values surrounded in parenthesis could make coding so much easier?

We’ve covered what Tuple swift are and how to utilize Tuple swift in this tutorial. However, if you want to know more about other information about Swift. Let’s check out the following articles:

ArrowHitech has become one of the superior IT outsourcing Vietnam company. We are experienced professionals in the field of  ECommerce, Web/mobile apps, CMS website development as well as Salesforce and Software Consultant & Development.