Search icon CANCEL
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 Proven recipes for developing robust iOS applications with Swift 5.9

Arrow left icon
Product type Paperback
Published in Jun 2024
Publisher Packt
ISBN-13 9781803239583
Length 422 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Chris Barker Chris Barker
Author Profile Icon Chris Barker
Chris Barker
Daniel Bolella Daniel Bolella
Author Profile Icon Daniel Bolella
Daniel Bolella
Nathan Lawlor Nathan Lawlor
Author Profile Icon Nathan Lawlor
Nathan Lawlor
Keith Moon Keith Moon
Author Profile Icon Keith Moon
Keith Moon
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Chapter 1: Swift Fundamentals 2. Chapter 2: Mastering the Building Blocks FREE CHAPTER 3. Chapter 3: Data Wrangling with Swift 4. Chapter 4: Generics, Operators, and Nested Types 5. Chapter 5: Beyond the Standard Library 6. Chapter 6: Understanding Concurrency in Swift 7. Chapter 7: Building iOS Apps with UIKit 8. Chapter 8: Building iOS Apps with SwiftUI 9. Chapter 9: Getting to Grips with Combine 10. Chapter 10: Using CoreML and Vision in Swift 11. Chapter 11: Immersive Swift with ARKit and Augmented Reality 12. Chapter 12: Visualizing Data with Swift Charts 13. Index 14. 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, 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:

  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) {
    }
  2. 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.
  3. Still within the function, take the rounded fraction and convert it from Float into 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)
  4. Call our new function and store the result in a constant:
    let ratingAndDisplayString = normalisedStarRating(forRating: 5,
    ofPossibleTotal: 10)
  5. 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
  6. 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.

You have been reading a chapter from
Swift Cookbook - Third Edition
Published in: Jun 2024
Publisher: Packt
ISBN-13: 9781803239583
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