Tuples - FUNDAMENTALS - Understanding Swift Programming: Swift 2 (2015)

Understanding Swift Programming: Swift 2 (2015)

PART 1: FUNDAMENTALS

8. Tuples

A tuple is a simple way to pass, or store, multiple pieces of data, particularly those of of different types. The initial motivation for tuples was probably the need to pass more than one return value from a function, especially when the values are of different types. An array cannot be used in this situation because all of the values in a Swift array must be of the same type. But this is easy to do with a tuple.

Tuples are generally used to do simple things.

A tuple normally consists of at least two values—variables, constants, or other types—contained within parentheses. Thus:

var returnThis = (283,"Error: Bad syntax")

A tuple can contain any combination of types, and any number of types.

The code above defines a tuple that has two components, an integer and a string. Its type is (Int, String), as we can see if we explicitly declare the variable:

var returnThis: (Int, String) = (283,"Error: Bad syntax")

A tuple can be accessed by position using dot syntax:

var errorCode = returnThis.0

var errorText = returnThis.1

A tuple can also define names for its components:

var returnThis = (code: 283, text: "Error: Bad syntax")

The values can then be retrieved using these names with dot syntax:

var errorCode = returnThis.code

var errorText = returnThis.text

Returning a Tuple from a Function

func getPlayerNameAndNumberOfHomeRunsThisYear() -> (name: String, runs: Int) {

var PlayerName = "Babe Ruth"

var PlayerRuns = 55

return (PlayerName, PlayerRuns)

}

This shows both the use of a tuple to return mixed values from a function, as well as setting that tuple from a variable.

The function has no input values and always returns the same thing. The result is a tuple with a value of ("Babe Ruth", 55).

let playerNameAndRuns = getPlayerNameAndNumberOfHomeRunsThisYear()

print(playerNameAndRuns.0) // Prints Babe Ruth

print(playerNameAndRuns.1) // Prints 55

Empty Tuple

It's possible to have an empty tuple, defined like this:

var empty = ()

Single Tuple—NOT!

A tuple containing a single value is theoretically not defined and makes no sense, as the compiler just ignores (or presumably should ignore) the parentheses:

var singleTuple = ("Babe Ruth")

Switch Statements

Tuples can be used as literals to match cases in switch statements.

var tup = (45,"Don Drysdale")

switch(tup) {

case (35,"Fernando Valenzuela"):

print("Valuenzuela with 35 home runs")

case (44,"Babe Ruth"):

print("Ruth with 44 home runs")

case (45,"Don Drysdale"):

print("Drysdale with 45 home runs")

default:

print("Can't find anything that matches")

}

Tuples can be used quite flexibly in switch statements. See Chapter 20, "The Flow of Control Revisited" for details.

Unusual Tricks with Tuples: Decomposition

The following will assign the values of 1, 2, and 3, respectively, to the variables a, b, and c:

var (a,b,c) = (1,2,3)

In other words, the above is equivalent to:

var a = 1

var b = 2

bar c = 3

This is known as decomposition.

How to Pronounce the Word "Tuple"

How should the word "tuple" be pronounced? It can be correctly pronounced in two ways, as suggested by the last part of the words "quadruple" and "quintuple". In other words, "too-puhl" or "tuh-puhl". This is in the United States. British English speakers may pronounce it slightly differently.

Hands-On Exercises

Go to the following web address with a Macintosh or Windows PC to do the Hands-On Exercises.

For Chapter 8 exercises, go to

understandingswiftprogramming.com/8