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 swift tuple – The definition and how you can use it in practical iOS development. Let’s discover.
A tuple (pronounced “TEW-ple”) is a list of values. Its Swift tuple values are ordered and separated by commas and surrounded in parentheses.
In this example, the tuple 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 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 Swift tuple 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 Swift tuple cannot be changed when it has been created. As a result, the example below are different:
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:
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 Swift tuple?
Swift tuple are highly lightweight data structures . Because it 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 Swift tuple
How to create a basic Swift tuple consisting of a integer and a string:
Because the compiler will figure it out for you, Swift tuple’s type definition can be ignored:
Individual elements can also be named to define a Swift tuple:
Reading values from tuple
Decompose a Swift tuple into individual variables or constants as follows:
With an underscore, you can ignore sections of the tuple:
Individual elements of the tuple can access using indexes:
You may also use element names if you defined the tuple by naming specific elements:
Using tuples 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:
The following is an example of how to read the return value of the getCity() function:
How to destructuring Swift tuple
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:
- let flight = (airport: “LAX”, airplane: 747, heading: 270)
- 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:
Interpret the example above:
- The tuple 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. 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 to get at the individual values (code, text). The values can then be printed using the individual constants code and text.
4. Further Reading
Tuples are a basic concept that has a lot of power. It is suprise that a few values surrounded in parenthesis could make coding so much easier?
We’ve covered what tuples are and how to utilize them in Swift in this tutorial. However, if you want to know more about other information about Swift. Let’s check out the following articles:
Another way, you can access the useful articles at ArrowHiTech Website