Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
tuples
communitycreator

What is iOS swift type -> tuple type?

Fortune Agu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

History of tuples in Swift

Tuples were introduced to swift in Swift 4 as a way to group many values into a single compound value. Using tuples is advantageous because the values do not need to be of the same type.

What are tuples in swift?

According to Apple, “a tuple type is a comma-separated list of zero or more types, enclosed in parentheses.

Tuples are a type of data structure that sits between dictionaries and structs. Tuples group multiple values into a single compound value. Multiple values can be returned from a function call using tuples.

We determine tuple type by the values it has. For example, ("Fahamag", 2021, false) will be of type (String, Int, Bool).

Creating a tuple

An example of a basic tuple:

let device = (name: "Apple iPhone", modelNumber: 8)

From this example, device.name and device.modelNumber can be read just like in a struct. Note that these two values are of different types, and this is allowed in tuples.

We can use a tuple to initialize more than one variable on a single line:

var (c, d, e) = (4, 5, 6)

Changing the value of a tuple

You can change the value of a tuple using the dot notation if it is declared as a variable:

var point = (0, "google")
point.0 = 1
point.1 = "yahoo"
print(point) //This prints (1, "yahoo")

Accessing the values of a tuple

Tuples can be accessed using the name of each element, and they also can be accessed using their position in the tuple, e.g., 0, 1, 2…

It is not entirely necessary to give your tuple elements names, but it is good practice to as it helps with readability, adds clarity, and avoids confusion, especially when you are working with complex tuples.

Another example showing how tuples can be accessed using either element names or positions in the tuple is as follows:

func split(name: String) -> (firstName: String, lastName: String) {
let split = name.components(separatedBy: " ")
return (split[0], split[1])
}
let parts = split(name: "Mahemmido Behira")
print(parts.0) //This prints "Mahemmido"
print(parts.1)//This prints "Behira"
print(parts.firstName) //This prints "Mahemmido"
print(parts.lastName) //This prints "Behira"

In this example a function splits the name “Mahemmido Behira” in two and returns a tuple containing the first name and the last name.

A tuple inside another tuple

let mrX: (Int, (Bool, String)) = (27, (true, "Jackson"))
print(mrX.0) // print: “27”
print(mrX.1.0) // print: “true”
print(mrX.1.1) // print: “Jackson”

Like structs, tuples are value types. When you initialize a variable tuple with another one, it will create a copy that bears no reference to the previous tuple.

var start = (x: 0.0, y: 0.0)
var point = start
point.x = 55.6
point.y = 23.1
print(start) // (x: 0.0, y: 0.0)
print(point) // (x: 55.6, y: 23.1)

Where to use tuples

Tuples are best used when you want a function to return multiple types.

Perusing this example highlights the limitation of collection types and why tuples is a better choice for returning multiple types:

func fetchPersonDetails() -> [String: Any] {
// ...
return ["name": "Fahamag", "age": 150, "height": 171]
}

This function might look okay, but it has a fundamental flaw. What if we decide to return the height as a string?

func fetchPersonDetails() -> [String: Any] {
// ...
return ["name": "Fahamag", "age": 150, "height": "171"]
}

Absolutely nothing will happen. And that is problematic. In the next example, we can see how using a tuple improves this.

Because we define the return value of the function with a tuple, we need to then conform to the tuple’s requirements. Anything else would throw an error.

When the types are strictly adhered to, the error goes away.

Note: It is important to note that tuples are designed for temporarily storing related data. If you must handle complex data structures, it is recommended to use classes and structs. Tuples are not collections even though they group values together.

RELATED TAGS

swift
tuples
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring