So as we realized in the last lesson, we need to be able to store two interrelated pieces of data, namely, 1
the first number that the user wants to calculate, and the calculation that they want to make inside 2
something that can group them together. 3
Now, we've learned about quite a few different things that Swift allows us to do. 4
But one thing we've yet to see that Swift offers is something called a tuple, and it's something that 5
allows us to group together related pieces of data that have different data types, and it's as convenient 6
as creating a simple dictionary, but it gives you the flexibility of holding different data types and 7
it's not as high effort as creating an entire struct or a class. 8
Now, here, I've created a brand-new playground and I've deleted all of the code other than import foundation. 9
And I can create a brand-new tuple. Let's call it touple1 and I can give it some data by simply opening 10
a set of parentheses. Inside the parentheses, 11
I can add in as many pieces of data as I want 12
separated by a comma. 13
So let's say Angela and 12, and there is our brand-new touple. 14
So if at some later point, I need to use one of the values inside my tuple, I can simply say touple1 15
.0. 16
And you can see that as soon as I start writing the dot, it tells me the first item in my tuple is a 17
string 18
and the second item is an integer. 19
And if I hit enter, then you can see that I've got the first item printed out done here. 20
So that's how you can access tuples like this. 21
Now, just having a 0 and 1 is not very explicit and it's quite difficult to see what's going on here 22
in the code. 23
So even though that's quite short, it's not the preferred way of creating a tuple. 24
Instead, what we can do is we can say let touple, let's call this one touple2 equal, and we give it some 25
parameter names, right? 26
So let's say name is equal to "Angela" 27
and age is equal to 12. 28
Now, fear not. 29
I am not a 12 years old recording videos on the Internet, but that was just the first number that 30
came to my mind. 31
Now, if we created our touples like this, then we can access the items in it by saying touple2 dot, and 32
we can tap into one of the names which is way more explicit in knowing what it is that we need. 33
So lets say touple.name. And in this case, again, we will get Angela printed down here. Now, the third 34
way of creating a touple is by creating a empty one first. 35
So let's call it touple3 and we can specify its data type as a touple, and it's going to have a parameter 36
called name which is a String, and also a parameter called age which is going to be an integer. And at a 37
later point, when we need to use this touple, we can simply say touple3.name is equal to "Angela" 38
touple3.age is equal to, let's say, 18. 39
Growing old really quickly. You can also if you want simply assign this in one go. 40
So you can say touple3 = name is "Angela," age is 21. 41
There we go. 42
So by using a Swift tuple, we're able to organize related pieces of data that are relatively small. 43
And we've been able to create this really, really quickly, pretty much on the fly. 44
And at a later stage, when we need to tap into any of these tuples, we can use the explicit structure of 45
a tuple by using the dot notation to use it anywhere inside our code. 46
So now that we've learned all about tuples, let's head back to our calculator to implement this in order to 47
store our first number and our operation together in the same tuple.