Bundling variables into tuples
A tuple is a combination of two or more values that can be treated as one. If you have ever wished you could return more than one value from a function or method, without defining a new struct or class, you should find tuples very interesting.
Getting ready
Create a new playground and add the following statement:
import Foundation
This example uses one function from Foundation
. We will delve into Foundation
in more detail in Chapter 5, Beyond the Standard Library, but for now, we just need to import it.
How to do it...
Let’s imagine that we are building an app that pulls movie ratings from multiple sources and presents them together, helping a user decide which movie to watch. These sources may use different rating systems, such as the following:
- The number of stars out of 5
- Points out of 10
- The percentage score
We want to normalize these ratings so that they can be compared directly and displayed side by side. We want all the ratings to be represented as the number of stars out of 5, so we will write a function that will return the number of whole stars out of 5. We will then use this to display the correct number of stars in our user interface (UI).
Our UI also includes a label that will read x Star Movie, where x is the number of stars. It would be useful if our function returned both the number of stars and a string that we can put in the UI. We can use a tuple to do this. Let’s get started:
- Create a function to normalize the star ratings. The following function takes a rating and a total possible rating, and then returns a tuple of the normalized rating and a string to display in the UI:
func normalizedStarRating(forRating rating: Float, ofPossibleTotal total: Float) -> (Int, String) { }
- Inside the function, calculate the fraction of the total score. Then, multiply that by our normalized total score,
5
, and round it up to the nearest whole number:let fraction = rating / total let ratingOutOf5 = fraction * 5 let roundedRating = round(ratingOutOf5) // Rounds to the nearest // integer.
- Still within the function, take the rounded fraction and convert it from
Float
intoInt
. Then, create the display string and return bothInt
andString
as a tuple:let numberOfStars = Int(roundedRating) // Turns a Float into an Int let ratingString = "\(numberOfStars) Star Movie" return (numberOfStars, ratingString)
- Call our new function and store the result in a constant:
let ratingAndDisplayString = normalisedStarRating(forRating: 5, ofPossibleTotal: 10)
- Retrieve the number of stars rating from the tuple and print the result:
let ratingNumber = ratingAndDisplayString.0 print(ratingNumber) // 3 - Use to show the right number of stars
- Retrieve the display string from the tuple and print the result:
let ratingString = ratingAndDisplayString.1 print(ratingString) // "3 Star Movie" - Use to put in the label
With that, we have created and used a tuple.
How it works...
A tuple is declared as a comma-separated list of the types it contains, within brackets. In the preceding section, in step 1, you can see a tuple being declared as (Int, String)
. The function, normalizedStarRating
, normalizes the rating and creates numberOfStars
as the closest round number of stars and ratingString
as a display string. These values are then combined into a tuple by putting them, separated by a comma, within brackets – that is, (numberOfStars, ratingString)
in step 3. This tuple value is then returned by the function.
Now, let’s look at what we can do with that returned tuple value:
let ratingAndDisplayString = normalizedStarRating(forRating: 5, ofPossibleTotal: 10)
Calling our function returns a tuple that we store in a constant called ratingAndDisplayString
. We can access the tuple’s components by accessing the numbered member of the tuple:
let ratingNumber = ratingAndDisplayString.0 print(ratingNumber) // 3 - Use to show the right number of stars let ratingString = ratingAndDisplayString.1 print(ratingString) // "3 Star Movie" - Use to put in the label
Note
As is the case with most numbered systems in programming languages, the member numbering system starts with 0
. The number that’s used to identify a certain place within a numbered collection is called an index.
There is another way to retrieve the components of a tuple that can be easier to remember than the numbered index. By specifying a tuple of variable names, each value of the tuple will be assigned to the respective variable names. Due to this, we can simplify accessing the tuple values and printing the result:
let (nextNumber, nextString) = normalizedStarRating(forRating: 8, ofPossibleTotal: 10) print(nextNumber) // 4 print(nextString) // "4 Star Movie"
Since the numerical value is the first value in the returned tuple, this gets assigned to the nextNumber
constant, while the second value, the string, gets assigned to nextString
. These can then be used like any other constant, eliminating the need to remember which index refers to which value.
There’s more...
As we mentioned previously, accessing a tuple’s components via a number is not ideal, as we have to remember their order in the tuple to ensure that we are accessing the correct one. To provide some context, we can add labels to the tuple components, which can be used to identify them when they are accessed. Tuple labels are defined in a similar way to parameter labels, preceding the type and separated by :
. Let’s add labels to the function we created in this recipe and then use them to access the tuple values:
func normalizedStarRating(forRating rating: Float, ofPossibleTotal total: Float) -> (starRating: Int, displayString: String) { let fraction = rating / total let ratingOutOf5 = fraction * 5 let roundedRating = round(ratingOutOf5) // Rounds to the nearest integer let numberOfStars = Int(roundedRating) // Turns a Float into an Int let ratingString = "\(numberOfStars) Star Movie" return (starRating: numberOfStars, displayString: ratingString) } let ratingAndDisplayString = normalizedStarRating(forRating: 5, ofPossibleTotal: 10) let ratingInt = ratingAndDisplayString.starRating print(ratingInt) // 3 - Use to show the right number of stars let ratingString = ratingAndDisplayString.displayString print(ratingString) // "3 Stars" - Use to put in the label
As part of the function declaration, we can see the tuple being declared:
(starRating: Int, displayString: String)
When a tuple of that type is created, the provided values are preceded by the label:
return (starRating: numberOfStars, displayString: ratingString)
To access the components of the tuple, we can use these labels (although the number of indexes still works):
let ratingValue = ratingAndDisplayString.starRating print(ratingValue) // 3 - Use to show the right number of stars let ratingString = ratingAndDisplayString.displayString print(ratingString) // "3 Stars" - Use to put in the label
Tuples are a convenient and lightweight way to bundle values together.
Tip
In this example, we created a tuple with two components. However, a tuple can contain any number of components.
See also
Further information about tuples can be found in Apple’s documentation on the Swift language at https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types.