Leave a rating/review
Notes: 11. Tuples
Update Notes: The student materials have been reviewed and are updated as of October 2021.
So far, you’ve been working with single pieces of data like an Integer or a String.
But sometimes data in your apps should be in pairs or triplets, or even larger groups, because the data has meaning when it’s all together.
In Swift, you can represent related pieces of data like this with a data type called a tuple.
You may also hear it pronounced like “toople” or “tuhple”, but no matter how you hear it pronounced, they’re all the same thing!
Open up the playground provided with this section of the course, find the “02 - Tuples” page, and start there!
Let’s create your first tuple in your playground! You’ll keep extending the student and mark examples that you worked with in the previous section of this course.
You define a tuple just like you would a constant. You start with let, then the name for your tuple:
let studentMark:
Then you have to declare the types that will be contained in your tuple. Now, the nice thing about tuples is that you can mix and match types as you need. In your previous examples, you had the student name stored as a string, and their mark stored as an integer.
So you simply use parentheses to surround the list of types in your tuple, in this case, a string and an integer:
let studentMark: (String, Int)
Then, you can tell Swift what actual values should be stored in your tuple:
let studentMark: (String, Int) = ("Chris", 49)
Now, you have a constant that has not one, but two pieces of information in there: the student name, and the student mark.
Now how do you get that information back out of the tuple?
Items in a tuple can be referenced by their position in the list, and they’re numbered starting from zero. In this case, you have the String in position zero, and the integer in position one:
// 0 1
let studentMark: (String, Int) = ("Chris", 49)
To get each of these values separately, you can just use the name of the tuple, followed by a dot, followed by the position, or index, of the value you want.
So to get the name of the student, you can say:
studentMark.0
…and to get the student’s mark, you can say:
studentMark.0
Now, it’s not very convenient to use ones and zeros to reference the individual members in your tuple. Wouldn’t it be nicer to use actual words instead of numbers?
Well, you can!
Let’s create a tuple that has three members: a name, a mark, and a pet.
First, let studentData, and then set that to, in parentheses, name, Chris; mark, 49, and petname, Mango:
let studentData = (name: "Chris", mark: 49, petName: "Mango")
You’ll notice that I didn’t specify the type of each member of the tuple here. That’s because Swift uses type interpolation to figure out the correct type for each member in here.
Now that I have named members of my tuple, it becomes a lot easier to reference each of them from code. Instead of the index of where a member lives in the tuple, you can simply use the name of the member:
let theName = studentData.name // gives you the name of the student
let theMark = studentData.mark // gives you the mark of the student
let thePetName = studentData.petName /// gives you the name of the student's pet
That’s handy for sure, but it can be a little tedious to pull each of those values out on separate lines. But you can use a shortcut trick to get those values into separate variables, all in one line of code!
You can do the following: let, and then in parentheses, declare your variables or constants, and then set that equal to the tuple name:
let (name, mark, pet) = studentData
In just one line of code, this creates three constants, name, mark and pet, and sets each of them to the value of the member in the same position inside the tuple.
So name gets the value at index zero, mark gets the value at index one, and so on.
name
mark
pet
There’s one last trick to show you. Say you only want to pull some of the values in a large tuple. In this case, we don’t care what mark Chris has, but we really do care about his pet!
You can still use that shortcut syntax, but replace the name for the element you don’t want with an underscore. So in this case, we don’t care about the mark, so we’ll say let, parentheses, name, underscore, pet, takes the value of student data.
let (name, �_, pet) = studentData
And then we see that this only declares two constants, name and pet, and ignores that middle value.
That’s it! That’s the basics of using tuples.
Tuples are great if you want to easily group a set of values together.
Later in this course, you’ll learn another way to combine values using structs and classes, but tuples are still useful when you need something to put some values together in a quick and temporary way.