0
1
00:00:00,540 --> 00:00:08,160
So as we realized in the last lesson, we need to be able to store two interrelated pieces of data, namely,
1

2
00:00:08,280 --> 00:00:14,610
the first number that the user wants to calculate, and the calculation that they want to make inside
2

3
00:00:14,730 --> 00:00:16,890
something that can group them together.
3

4
00:00:16,950 --> 00:00:21,880
Now, we've learned about quite a few different things that Swift allows us to do.
4

5
00:00:22,200 --> 00:00:29,220
But one thing we've yet to see that Swift offers is something called a tuple, and it's something that
5

6
00:00:29,220 --> 00:00:37,380
allows us to group together related pieces of data that have different data types, and it's as convenient
6

7
00:00:37,530 --> 00:00:43,830
as creating a simple dictionary, but it gives you the flexibility of holding different data types and
7

8
00:00:43,830 --> 00:00:48,570
it's not as high effort as creating an entire struct or a class.
8

9
00:00:48,570 --> 00:00:54,480
Now, here, I've created a brand-new playground and I've deleted all of the code other than import foundation.
9

10
00:00:55,100 --> 00:01:02,880
And I can create a brand-new tuple. Let's call it touple1 and I can give it some data by simply opening
10

11
00:01:02,910 --> 00:01:05,700
a set of parentheses. Inside the parentheses,
11

12
00:01:05,760 --> 00:01:08,700
I can add in as many pieces of data as I want
12

13
00:01:08,730 --> 00:01:10,490
separated by a comma.
13

14
00:01:10,620 --> 00:01:17,550
So let's say Angela and 12, and there is our brand-new touple.
14

15
00:01:17,580 --> 00:01:25,500
So if at some later point, I need to use one of the values inside my tuple, I can simply say touple1
15

16
00:01:26,020 --> 00:01:26,860
.0.
16

17
00:01:27,330 --> 00:01:32,610
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

18
00:01:32,610 --> 00:01:33,360
string
18

19
00:01:33,540 --> 00:01:35,670
and the second item is an integer.
19

20
00:01:35,880 --> 00:01:41,150
And if I hit enter, then you can see that I've got the first item printed out done here.
20

21
00:01:41,340 --> 00:01:44,100
So that's how you can access tuples like this.
21

22
00:01:44,100 --> 00:01:50,780
Now, just having a 0 and 1 is not very explicit and it's quite difficult to see what's going on here
22

23
00:01:50,810 --> 00:01:51,680
in the code.
23

24
00:01:51,900 --> 00:01:56,890
So even though that's quite short, it's not the preferred way of creating a tuple.
24

25
00:01:56,990 --> 00:02:04,500
Instead, what we can do is we can say let touple, let's call this one touple2 equal, and we give it some
25

26
00:02:04,500 --> 00:02:05,910
parameter names, right?
26

27
00:02:05,910 --> 00:02:10,320
So let's say name is equal to "Angela"
27

28
00:02:10,440 --> 00:02:13,390
and age is equal to 12.
28

29
00:02:13,770 --> 00:02:14,660
Now, fear not.
29

30
00:02:14,790 --> 00:02:20,790
I am not a 12 years old recording videos on the Internet, but that was just the first number that
30

31
00:02:20,790 --> 00:02:22,340
came to my mind.
31

32
00:02:22,350 --> 00:02:28,890
Now, if we created our touples like this, then we can access the items in it by saying touple2 dot, and
32

33
00:02:28,890 --> 00:02:34,330
we can tap into one of the names which is way more explicit in knowing what it is that we need.
33

34
00:02:34,380 --> 00:02:42,780
So lets say touple.name. And in this case, again, we will get Angela printed down here. Now, the third
34

35
00:02:42,780 --> 00:02:48,320
way of creating a touple is by creating a empty one first.
35

36
00:02:48,420 --> 00:02:57,560
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

37
00:02:57,560 --> 00:03:05,520
called name which is a String, and also a parameter called age which is going to be an integer. And at a
37

38
00:03:05,520 --> 00:03:16,350
later point, when we need to use this touple, we can simply say touple3.name is equal to "Angela"
38

39
00:03:18,670 --> 00:03:25,120
touple3.age is equal to, let's say, 18.
39

40
00:03:25,240 --> 00:03:30,570
Growing old really quickly. You can also if you want simply assign this in one go.
40

41
00:03:30,610 --> 00:03:41,860
So you can say touple3 = name is "Angela," age is 21.
41

42
00:03:41,870 --> 00:03:42,560
There we go.
42

43
00:03:42,800 --> 00:03:49,810
So by using a Swift tuple, we're able to organize related pieces of data that are relatively small.
43

44
00:03:50,000 --> 00:03:55,370
And we've been able to create this really, really quickly, pretty much on the fly.
44

45
00:03:55,520 --> 00:04:01,910
And at a later stage, when we need to tap into any of these tuples, we can use the explicit structure of
45

46
00:04:01,910 --> 00:04:07,220
a tuple by using the dot notation to use it anywhere inside our code.
46

47
00:04:07,220 --> 00:04:14,180
So now that we've learned all about tuples, let's head back to our calculator to implement this in order to
47

48
00:04:14,180 --> 00:04:18,830
store our first number and our operation together in the same tuple.
