0
1
00:00:01,010 --> 00:00:04,190
Are you ready for another Swift Deep Dive? In this lesson,
1

2
00:00:04,190 --> 00:00:10,730
I want to talk about Arrays and Arrays are essentially just collections of items,
2

3
00:00:10,730 --> 00:00:16,670
so whereas variables, you have a name that's associated with a single piece of data. What do you do if
3

4
00:00:16,670 --> 00:00:23,600
you have lots of data that are somehow associated with each other like a basket of apples?
4

5
00:00:23,600 --> 00:00:27,310
Well, that is kind of like an array of apples, right?
5

6
00:00:27,410 --> 00:00:29,320
Creating an Array is pretty simple.
6

7
00:00:29,330 --> 00:00:36,230
We use these square brackets. And inside the brackets, we drop in all the items that we want to put into
7

8
00:00:36,230 --> 00:00:41,260
the same collection or the same Array, and we separate them by commas.
8

9
00:00:41,270 --> 00:00:46,870
So we now have an Array of names and we have three items in our Array.
9

10
00:00:47,030 --> 00:00:54,470
Now, when we want to access items from an array, then we can use another set of square brackets and we
10

11
00:00:54,470 --> 00:00:59,630
tag it on at the end of the Array. And inside the square brackets,
11

12
00:00:59,660 --> 00:01:07,100
that X is a number that refers to the position of the item that we want to retrieve from the Array.
12

13
00:01:07,100 --> 00:01:12,440
And if you remember from previous lessons, we said that programmers love counting from zero.
13

14
00:01:12,830 --> 00:01:20,840
So the first item that Angela is at position 0, Jack is at position 1, and Philip is at position 2.
14

15
00:01:21,080 --> 00:01:29,180
So if we replace the X with 0, 1, or 2, then we will be able to retrieve a single item out of our Array
15

16
00:01:29,510 --> 00:01:32,700
depending on which position we specified as x.
16

17
00:01:32,750 --> 00:01:41,150
So just as we can store other pieces of data, such as numbers or text in a variable, an array is just
17

18
00:01:41,150 --> 00:01:46,880
like any other piece of data and it, too, can be stored inside a Variable.
18

19
00:01:46,880 --> 00:01:53,360
Let's say that we had a Variable code friends, we can assign it the value of our Array.
19

20
00:01:53,930 --> 00:02:00,630
So now we want to access this Array, we can refer to that reference code friends.
20

21
00:02:00,630 --> 00:02:06,500
And, again, when we're accessing items from an Array that's been stored inside a Variable,
21

22
00:02:06,500 --> 00:02:08,200
it's exactly the same thing.
22

23
00:02:08,240 --> 00:02:14,690
Whereas before we had a straight up Array and we tagged on at the end a set of square brackets and a
23

24
00:02:14,690 --> 00:02:19,290
number that referred to the position of the item that we wanted to access.
24

25
00:02:19,310 --> 00:02:24,110
Well, if we had the array stored in a variable, it would look like this.
25

26
00:02:24,140 --> 00:02:29,900
We would refer to the name of the array and then we would, again, tag a set of square brackets at the
26

27
00:02:29,900 --> 00:02:35,930
end, and the X refers to the position of the item that we're interested in from the Array.
27

28
00:02:35,990 --> 00:02:43,250
The easiest way to think about an array is to imagine you had a spreadsheet where you only have a single
28

29
00:02:43,250 --> 00:02:44,240
column.
29

30
00:02:44,240 --> 00:02:51,440
For example, here, I have an array of famous people and I've got a whole bunch of names in my Array and
30

31
00:02:51,560 --> 00:02:53,120
they are numbered.
31

32
00:02:53,240 --> 00:02:57,110
So in the case of a spreadsheet, they're numbered from 1 to 10.
32

33
00:02:57,230 --> 00:03:04,940
But if this was an Array, then this would be at position 0, 1, 2, et cetera. It's essentially a single column
33

34
00:03:05,240 --> 00:03:11,780
with a whole bunch of data in it and each piece of data has a position associated with it.
34

35
00:03:11,870 --> 00:03:14,670
So that's just another way to think about Arrays.
35

36
00:03:14,720 --> 00:03:19,910
And if you've worked with spreadsheet delight, then this might be a really easy way for you to understand
36

37
00:03:19,910 --> 00:03:22,220
and visualize Arrays.
37

38
00:03:22,340 --> 00:03:24,950
So I've got a challenge question for you.
38

39
00:03:24,950 --> 00:03:26,760
Why did the programmer quit his job?
39

40
00:03:26,960 --> 00:03:30,100
Well, because he didn't get a raise, of course.
40

41
00:03:30,180 --> 00:03:33,420
Now, that's not the actual programming challenge.
41

42
00:03:33,440 --> 00:03:38,270
Instead, you're going to be writing some code to test your knowledge on Arrays.
42

43
00:03:38,360 --> 00:03:44,800
Now, the Array that I'm going to create is gonna be stored inside a Variable called numbers and I'm gonna
43

44
00:03:44,840 --> 00:03:46,950
set it to equal a bunch of numbers.
44

45
00:03:46,970 --> 00:03:53,500
So that's a 45. 73, 195, and 53.
45

46
00:03:53,540 --> 00:03:56,070
So I have an array of four numbers.
46

47
00:03:56,180 --> 00:04:02,910
Again, if you need to refer to how Arrays are structured, namely what kind of bracket does it use,
47

48
00:04:02,930 --> 00:04:05,710
and how do you separate each of the values in it,
48

49
00:04:05,780 --> 00:04:10,880
then you can take a look at the Swift Cheat Sheet, and remind yourself of how you create Variables and
49

50
00:04:11,210 --> 00:04:13,480
how you create Arrays.
50

51
00:04:13,490 --> 00:04:22,580
Now, I'm going to create another Variable called computedNumbers and I want you to write some code, so
51

52
00:04:22,580 --> 00:04:31,880
that when you print the computed numbers, the end result that you should see down here is the first number
52

53
00:04:31,940 --> 00:04:35,470
multiplied by the second, as the first value,
53

54
00:04:35,720 --> 00:04:42,560
and then it's the second number multiplied by the third, and finally, it's the third multiplied by the fourth.
54

55
00:04:42,560 --> 00:04:51,380
So let's say we had an array of 1, 2, 3, and 4, then the outcome that should be printed is 1 times 2 equals
55

56
00:04:51,380 --> 00:04:56,690
2, 2 times 3 is 6, and 3 times 4 is 12.
56

57
00:04:56,810 --> 00:05:04,010
So that would be the desired outcome. But instead, you're going to be working with these large numbers.
57

58
00:05:04,180 --> 00:05:11,100
And I don't want you to actually take a calculator and work out what is 45 times 73, and then what's 73
58

59
00:05:11,110 --> 00:05:12,830
times 195.
59

60
00:05:12,850 --> 00:05:20,260
And in fact, I don't even want you to write any code that includes those numbers 45, 73, 195, and 53.
60

61
00:05:20,290 --> 00:05:28,090
I want you to use purely just your knowledge on how to pull items out of the Array and use the Swift
61

62
00:05:28,150 --> 00:05:31,360
program to calculate these values for you.
62

63
00:05:31,870 --> 00:05:38,440
Now, in order to complete this challenge, you'll need to know that to multiply a number, say, 1 times 2,
63

64
00:05:38,950 --> 00:05:42,460
in Swift, It's done using the asterisk symbol.
64

65
00:05:42,760 --> 00:05:49,060
So if I print 1 * 2, and let me just coming out the code that's currently incomplete which is giving
65

66
00:05:49,060 --> 00:05:55,050
us some errors, then once my code runs you'll see 2, or 5 times 2 which should be 10.
66

67
00:05:55,060 --> 00:06:02,500
So multiply the value at position 0 by the value at position 1, and then the 1 position 1 by the one
67

68
00:06:02,500 --> 00:06:09,910
at position 2, and you should end up with an Array of computed numbers which is going to be printed and
68

69
00:06:09,910 --> 00:06:13,260
it should have three items in that Array.
69

70
00:06:13,450 --> 00:06:19,360
And I'll see you on the other side. Good luck!
