Are you ready for another Swift Deep Dive? In this lesson, 1
I want to talk about Arrays and Arrays are essentially just collections of items, 2
so whereas variables, you have a name that's associated with a single piece of data. What do you do if 3
you have lots of data that are somehow associated with each other like a basket of apples? 4
Well, that is kind of like an array of apples, right? 5
Creating an Array is pretty simple. 6
We use these square brackets. And inside the brackets, we drop in all the items that we want to put into 7
the same collection or the same Array, and we separate them by commas. 8
So we now have an Array of names and we have three items in our Array. 9
Now, when we want to access items from an array, then we can use another set of square brackets and we 10
tag it on at the end of the Array. And inside the square brackets, 11
that X is a number that refers to the position of the item that we want to retrieve from the Array. 12
And if you remember from previous lessons, we said that programmers love counting from zero. 13
So the first item that Angela is at position 0, Jack is at position 1, and Philip is at position 2. 14
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
depending on which position we specified as x. 16
So just as we can store other pieces of data, such as numbers or text in a variable, an array is just 17
like any other piece of data and it, too, can be stored inside a Variable. 18
Let's say that we had a Variable code friends, we can assign it the value of our Array. 19
So now we want to access this Array, we can refer to that reference code friends. 20
And, again, when we're accessing items from an Array that's been stored inside a Variable, 21
it's exactly the same thing. 22
Whereas before we had a straight up Array and we tagged on at the end a set of square brackets and a 23
number that referred to the position of the item that we wanted to access. 24
Well, if we had the array stored in a variable, it would look like this. 25
We would refer to the name of the array and then we would, again, tag a set of square brackets at the 26
end, and the X refers to the position of the item that we're interested in from the Array. 27
The easiest way to think about an array is to imagine you had a spreadsheet where you only have a single 28
column. 29
For example, here, I have an array of famous people and I've got a whole bunch of names in my Array and 30
they are numbered. 31
So in the case of a spreadsheet, they're numbered from 1 to 10. 32
But if this was an Array, then this would be at position 0, 1, 2, et cetera. It's essentially a single column 33
with a whole bunch of data in it and each piece of data has a position associated with it. 34
So that's just another way to think about Arrays. 35
And if you've worked with spreadsheet delight, then this might be a really easy way for you to understand 36
and visualize Arrays. 37
So I've got a challenge question for you. 38
Why did the programmer quit his job? 39
Well, because he didn't get a raise, of course. 40
Now, that's not the actual programming challenge. 41
Instead, you're going to be writing some code to test your knowledge on Arrays. 42
Now, the Array that I'm going to create is gonna be stored inside a Variable called numbers and I'm gonna 43
set it to equal a bunch of numbers. 44
So that's a 45. 73, 195, and 53. 45
So I have an array of four numbers. 46
Again, if you need to refer to how Arrays are structured, namely what kind of bracket does it use, 47
and how do you separate each of the values in it, 48
then you can take a look at the Swift Cheat Sheet, and remind yourself of how you create Variables and 49
how you create Arrays. 50
Now, I'm going to create another Variable called computedNumbers and I want you to write some code, so 51
that when you print the computed numbers, the end result that you should see down here is the first number 52
multiplied by the second, as the first value, 53
and then it's the second number multiplied by the third, and finally, it's the third multiplied by the fourth. 54
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
2, 2 times 3 is 6, and 3 times 4 is 12. 56
So that would be the desired outcome. But instead, you're going to be working with these large numbers. 57
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
times 195. 59
And in fact, I don't even want you to write any code that includes those numbers 45, 73, 195, and 53. 60
I want you to use purely just your knowledge on how to pull items out of the Array and use the Swift 61
program to calculate these values for you. 62
Now, in order to complete this challenge, you'll need to know that to multiply a number, say, 1 times 2, 63
in Swift, It's done using the asterisk symbol. 64
So if I print 1 * 2, and let me just coming out the code that's currently incomplete which is giving 65
us some errors, then once my code runs you'll see 2, or 5 times 2 which should be 10. 66
So multiply the value at position 0 by the value at position 1, and then the 1 position 1 by the one 67
at position 2, and you should end up with an Array of computed numbers which is going to be printed and 68
it should have three items in that Array. 69
And I'll see you on the other side. Good luck!