Tuples in Swift

We can group different values into a single value. What can possibly go wrong?

Tuples in Swift allows you to store multiple values in a single variable. Since we a representing multiple values as a single variable, it only really makes sense if the constituent parts are tightly connected.

Image for post
Image for post
Photo by Evelyn on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)

Terminology

Data Types: A representation of the tyoe of data that can be processed, for example Integer or String

Tuple: A way in Swift to allow you to store multiple values in a single variable.

Storing multiple values

Using a Struct

One possible solution to storing multiple values is to use a Struct

struct Person {
var name: String
var age: Int
}
let steve = Person(name: "Steve", age: 22)

Now, as you can see this is quite long and verbose. That is not to say that there are not uses for creating a Class or a Struct to store this data — but like many things in programming this is a question of style and taste.

Now of course a struct can also conform to protocols and methods can be written within the structure so really flexibility is one of the keys here.

The struct needs to be created in code, so you would need to know what data needs to be stored ahead of time.

Using an Array

You might want to use an array to store the details about a person.

However you can’t mix types in an Array

let steve = ["Steve", 22] // Error
Image for post
Image for post

So one alternative is to store the elements as Strings, and the elements can be written out by indexing them:

let steve = ["Steve", "22"]
print (steve) // ["Steve", "22"]
print (Steve[0]) // "Steve"

This isn’t great if we want to store more complex data types.

Thinking about this though, you can store an Array of Any:

let steve: [Any] = ["Steve", 22]
print (steve) // ["Steve", 22]
print (Steve[0]) // "Steve"

However whenever Any is the static type checking of Swift is lost, as well

Using a Tuple

You can structure a Tuple like in the following example:

var person = ("Steve", 22)

Note that each parameter is separated by a comma , and enclosed in parenthesis ().

To print out all the elements of the Tuple is actually quite easy:

print(person) // ("Steve", 22)

We can then access the properties within the Tuple by using the index value of such:

person.0 // Steve
person.1 // 22

Which is not very descriptive. Luckily, Swift has some help for us to be able to retrieve the parameters in a much better way — but there is a limitation, we must use labels for the dat within the Tuple.

So we have another example (very similar to the last, but still)

var dog = (name: "Bear", dangerous: "false")
dog.name // Bear
dog.dangerous // false

Limitations

In order to reference data with labels the data must have labels (this is shown above).

Swift is unable to create a single-element tuple with an element label:

var car = (name: "Bear") // error
Image for post
Image for post

and Swift gives you the option of removing the label in this case.

Advantages and disadvantages

Tuples are effectively just an anonymous struct that contains the advantages and disadvantages of a struct, but it is actually often better to use a struct and implement an array of that struct to get the advantages of both.

Advantages

You can’t move out of bounds like you can with arrays! This is a great feature and can stop those annoying crashes.

Naming the data components on the Tuples is very advantageous and can make the use of Tuples really quite appealing.

In terms of memory this is much better than using an array with Any which is both weakly typed an also unable to use the best memory tricks that the Swift compiler can come up with.

Disadvantages

Well, you should really use a struct and an array of the struct instead. To do so would look like:

struct Person {
var name: String
var age: Int
}
let steve = Person(name: "Steve", age: 22)
let james = Person(name: "James", age: 34)
let arr = [steve, james]

Conclusion

Tuples can be used to create straight and to the point code that is easy to read. Yes, you might often be better off using different Data Types but it depends on you context and the best choice for the problem that you want to solve.

Want to get in contact? Try the link here:

https://twitter.com/stevenpcurtis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store