In this tutorial, you will learn about tuples in Swift with its usage and examples.
In Swift, a tuple is used to represent a group of information or values that can be different types. In a tuple, it is not necessary to have the same types of data. You can store multiple types of data in a tuple.
For example, if you want to store information about a person with his name and age, then we can store the name as a string and the age as an integer like this:
Tuples are useful when you need additional information than just a single value return from a function. It is able to contain a group of related information.
Creating a tuple is an easy task in Swift programming. To store the pieces of information, we use the parenthesis in a tuple.
Below is the syntax of creating a tuple:
var NameOfTuple = (Value1, Value2, Value3… ValueN)
Below is an example of a tuple:
var person = ("Elias", 37)
In the above example, we have created a tuple that contains a string and an integer type of data.
Now let’s see how you can access elements of a tuple.
Accessing tuple elements is the same as accessing array elements. Just like an array, you can also access elements of a tuple by its index number. Elements of a tuple are also represented by its index number.
The first element of the tuple has the index number 0 and the second element has the index number 1 and so on just like we can see in an array.
If you want to access the first element from the tuple we created then we can do it as you can see below:
To access the second element:
Now see the complete example of creating a tuple and accessing its element by index number:
var person = ("Elias", 37) // Accessing the first element print("Name:",person.0) //Accessing the second element print("Age:",person.1)
You can modify the value of a tuple element by assigning a new value to the element.
Below is an example in Swift where we are assigning a new value for an element of a tuple to modify its value:
var person = ("Elias", 37) // print tuple before modifying print(person) // Modifying the second element value person.1 = 39 // Print tuple to check if our second element value has modified print(person)
From the output, we can see that our tuple element has been changed. In our example, we have changed the value of the second element.
In Swift, adding and removing tuple elements is not possible. It is not allowed.
If you try to add or remove the tuple element using the insert() and remove() method, it will show an error. Let’s see what happens if we do this:
var person = ("Elias", 37) person.insert("London") person.remove(37)
error: CodeSpeedy.playground:3:8: error: value of tuple type '(String, Int)' has no member 'insert'
~~ ^ ~
error: CodeSpeedy.playground:5:8: error: value of tuple type '(String, Int)' has no member 'remove'
~~ ^ ~
You can see you will get some errors if you try to add or remove elements to the tuple.
A nested tuple is a tuple that is the element of another tuple. You can think of a tuple inside a tuple.
In Swift, we can create nested tuples. Below is the example of a nested tuple:
var nestedTuple = ("apple", "mango", "pineapple", ("banana", "strawberry"))
In the above example,
strawberry are in a nested tuple.
You can give names for each element in a tuple. These names can be used to access the element. It will let you choose the element by its name.
So not only by using index number, but also you can get the tuple element value by its name.
Below is the syntax of providing the name of tuple elements:
var NameOfTuple = (Name1: Value1, Name2: Value2, Name3: Value3… NameN: ValueN)
Below is the example to show how you can give the names to the tuple elements:
var person = (name: "Elias", age: 37)
Below is another example of accessing tuple elements by their name:
var person = (name: "Elias", age: 37) // Accessing the name print("Name:",person.name) //Accessing the age print("Age:",person.age)
Name: Elias Age: 37
Giving names to elements can be so useful. For example, you can store weather data with the names like temperature, wind speed, current weather condition with the corresponding values.