SoFunction
Updated on 2025-04-11

Tuples of Swift Study Notes

Tuples

Tuples are types 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 passing, not references.

The way to create tuples in Swift is simple. Tuple types are a list of zero or more types surrounded by brackets and separated by a comma. For example:

let firstHighScore = ("Mary", 9001)

In addition, when creating a tuple, you can also name the elements in the tuple:

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

The above are two ways to create tuples, which are very simple and concise. You don't need to write out its structure and internal properties like creating a struct, nor do you need to write initialization methods like creating a class. You just need to put any type of value you want to use in parentheses and separate it with commas. If you want, you can also name each element to improve the efficiency of tuple usage.

Read elements from tuple

If we do not name the element of the tuple, we can use dot syntax to get its 1st to nth element by defining the tuple variable or constant:

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

If you think the above method will cause semantic ambiguity, then we can also assign the tuple to a tuple with the element name (the number of element names must correspond to):

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

If you only need a portion of the tuple value, you can underline the part you want to ignore when decomposing:

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

If we have named the elements in the tuple, we can write this:

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

Return a tuple as a function

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

We can use tuples as the return value of the function. The return value of the following function is the secondHighScore tuple we defined before:

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

Why is the return value of the above function called a secondHighScore tuple? Because the number of tuple elements, element name, and element type returned by the getAHighScore function are the same as secondHighScore.

In fact, when using tuples as the return value of a function, you don’t have to name elements, as long as you understand the meaning of each element:

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

If you are not sure the tuple returned must not be nil, then you can return an optional tuple type:

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

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

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

Notice:When you define a function that has no return value, the function actually returns an empty tuple().

Access level of tuples

The access level of a tuple depends on the elements it contains. For example, if the elements in a tuple are all private level, then the tuple is also private level. But there is a principle that follows the minimum, that is, if there are two elements in a tuple, one is private level and the other is public level, then the tuple follows the minimum principle and its access level is private.

Tuple is a value type

The knowledge about value types and reference types is no longer cumbersome here. Let’s use a code example to see which type of tuple is:

var someScore = ("John", 55)
var anotherScore = someScore
anotherScore.0 = "Robert"
println(anotherScore.0) //Outputs: "Robert"
println(someScore.0)  //Outputs: "John"

From the above code example, we can see that I assigned the someScore tuple to anotherScore, then modified the value of the first element of anotherScore, and finally printed the values ​​of someScore and anotherScore respectively. The value of the first element of someScore tuple is Robert, while the value of the first element of anotherScore tuple is still John. It can be seen that the tuple is a value type.

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.