Swift Tuples of Study Notes (tuples)

  • 2020-06-03 08:32:32
  • OfStack


A tuple (tuples) is a type composed of other types. Tuples may contain zero or more types, such as strings, integers, characters, booleans, and other tuples. Also note that tuples are value passes, not references.

The way to create tuples in Swift is simple. Tuples are lists of zero or more types enclosed in parentheses and separated by 1 comma. Such as:

let firstHighScore = ("Mary", 9001)

In addition, when creating tuples, you can also name elements in tuples:

let secondHighScore = (name: "James", score: 4096)

So those are the two ways to create tuples, very simple and very concise. You don't need to write its structure and internal properties as you did with struct1, nor do you need to write the initialization methods as you did with class1. You just put whatever type of value you want in parentheses, separated by commas. You can also name each element if you wish, making tuples more efficient.

Reads elements from tuples

If we don't name the elements of a tuple, we can use dot syntax to get its first to n elements by defining tuple variables or constants:

let firstHighScore = ("Mary", 9001)
println(firstHighScore.0) // Mary
println(firstHighScore.1) // 9001

If you feel that this approach is causing semantic ambiguity, you can also assign a tuple to a tuple with an element name:

let (firstName, firstScore) = firstHighScore
println(firstName) // Mary
println(firstScore) // 9001

If you only need 1 part of a tuple, you can underline (_) the parts you want to ignore when factoring:

let (_, firstScore) = firstHighScore
println(firstScore) // 9001

If we have given a name to an element in a tuple, we can write this:

let secondHighScore = (name: "James", score: 4096)
println(secondHighScore.name) // James
println(secondHighScore.score) // 4096

Returns a tuple as a function value

This is the best use case for tuples when you want one function to return multiple types.

The return value of this function is the secondHighScore tuple we defined earlier:

func getAHighScore() -> (name: String, score: Int) {
 let theName = "Patricia"
 let theScore = 3894
 return (theName, theScore)

Why is the return value of the above function secondHighScore tuple? Because the getAHighScore function returns the same number of tuple elements, element names, and element types as secondHighScore.

You don't have to name the elements when using tuples as the return value of a function, as long as you understand what each element represents:

func getAHighScore() -> (String, Int) {
 let theName = "Patricia"
 let theScore = 3894
 return (theName, theScore)

If you are not sure if the tuple 1 returned is nil, you can return an optional tuple type:

func maybeGetHighScore() -> (String, Int)? {
 return nil

Since it is an optional tuple type, you need to unpack the tuple when the returned tuple is not nil:

if let possibleScore = maybeGetHighScore() {
} else {
 println("Nothing Here")

Note: When you define a function that returns no value, the function returns an empty tuple ().

The access level of a tuple

The access level of a tuple depends on the elements it contains. If the elements in a tuple are private level, the tuple is private level. But there is one that follows the minimum rule, which means that if there are two elements in a tuple, one at private and one at public, then the tuple follows the minimum rule and has an access level of private.

Tuples are value types

Knowledge of value types and reference types is no longer cumbersome here, but let's look at a code example to see what tuples are:

let secondHighScore = (name: "James", score: 4096)

As you can see from the above code example, I assigned the someScore tuple to anotherScore, then modified the value of the first element of anotherScore, and finally printed the value of the first element of someScore and anotherScore, respectively. The value of the first element of the someScore tuple is Robert, while the value of the first element of the anotherScore tuple is still John. So tuples are value types.


Related articles: