Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Swift Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
Swift Cookbook

Swift Cookbook

By : Keith Moon, D. Moon, Chris Barker
5 (10)
close
close
Swift Cookbook

Swift Cookbook

5 (10)
By: Keith Moon, D. Moon, Chris Barker

Overview of this book

Swift is an exciting, multi-platform, general-purpose programming language, and with this book, you'll explore the features of its latest version, Swift 5.3. The book begins with an introduction to the basic building blocks of Swift 5.3, its syntax, and the functionalities of Swift constructs. You’ll then discover how Swift Playgrounds provide an ideal platform to write, execute, and debug your Swift code. As you advance through the chapters, the book will show you how to bundle variables into tuples or sets, order your data with an array, store key-value pairs with dictionaries, and use property observers. You’ll also get to grips with the decision-making and control structures in Swift, examine advanced features such as generics and operators, and explore functionalities outside of the standard library. Once you’ve learned how to build iOS applications using UIKit, you'll find out how to use Swift for server-side programming, run Swift on Linux, and investigate Vapor. Finally, you'll discover some of the newest features of Swift 5.3 using SwiftUI and Combine to build adaptive and reactive applications, and find out how to use Swift to build and integrate machine learning models along with Apple’s Vision Framework. By the end of this Swift book, you'll have discovered solutions to boost your productivity while developing code using Swift 5.3.
Table of Contents (14 chapters)
close
close
12
About Packt

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.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY