Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift Cookbook

You're reading from   Swift Cookbook Over 60 proven recipes for developing better iOS applications with Swift 5.3

Arrow left icon
Product type Paperback
Published in Feb 2021
Publisher Packt
ISBN-13 9781839211195
Length 500 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Chris Barker Chris Barker
Author Profile Icon Chris Barker
Chris Barker
Keith D. Moon Keith D. Moon
Author Profile Icon Keith D. Moon
Keith D. Moon
Keith Moon Keith Moon
Author Profile Icon Keith Moon
Keith Moon
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Swift Building Blocks 2. Mastering the Building Blocks FREE CHAPTER 3. Data Wrangling with Swift Control Flow 4. Generics, Operators, and Nested Types 5. Beyond the Standard Library 6. Building iOS Apps with Swift 7. Swift Playgrounds 8. Server-Side Swift 9. Performance and Responsiveness in Swift 10. SwiftUI and Combine Framework 11. Using CoreML and Vision in Swift 12. About Packt 13. Other Books You May Enjoy

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, 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 to help the user decide which movie to watch. These sources may use different rating systems, such as the following:

  • Number of stars out of 5
  • Points out of 10
  • 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 a 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:

  1. 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) {

}
  1. Inside the function, calculate the fraction of the total score. Then, multiply that by our normalized total score, 5, and round it to the nearest whole number:
let fraction = rating / total 
let ratingOutOf5 = fraction * 5
let roundedRating = round(ratingOutOf5) // Rounds to the nearest
// integer.
  1. Still within the function, take the rounded fraction and convert it from a Float into an Int. Then, create the display string and return both Int and String as a tuple:
let numberOfStars = Int(roundedRating) // Turns a Float into an Int 
let ratingString = "\(numberOfStars) Star Movie"
return (numberOfStars, ratingString)
  1. Call our new function and store the result in a constant:
let ratingAndDisplayString = normalisedStarRating(forRating: 5, 
ofPossibleTotal: 10)
  1. 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
  1. 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 code, 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). This tuple value is then returned by the function.

Next, 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
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 and removes 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 a :. Let's add labels to the function we created in this recipe and then use those labels 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 work):

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.

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/ReferenceManual/Types.html.

You have been reading a chapter from
Swift Cookbook - Second Edition
Published in: Feb 2021
Publisher: Packt
ISBN-13: 9781839211195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime