It's time for yet another Swift Deep Dive. And in this Deep Dive, we're going to look at variables. 1
So essentially we've seen that variables are simply a way of giving a name to a piece of data so that 2
we can reference it in our code later on. 3
We did that for our IBOutlets as well as our DiceNumber variable which we created from scratch. 4
And the problem that it solves is really simple. 5
Let's say that if I was to give you a phone number to remember. And I give it to you written down on 6
a piece of paper. 7
Now, if all I had on there was just a number, I know what my drawers look like. 8
And if I have a single piece of paper floating around in there, after not even a day, I'll forget what 9
that number was all about. 10
So how can we make this better? 11
How can we remember what this piece of data is about? 12
We could associate that data with an actual label and give it a name. 13
So we're essentially taking our data and labelling it with a name. 14
Well, this is essentially a variable because if we needed that piece of data in the future, we can 15
simply refer to the label name, which is "Angela", and it will be able to retrieve that piece of data. 16
So we have to write a little bit more 17
if we're creating this code. We have to add a "var" keyword, which tells the computer that we're creating 18
a new variable. And then we add an equal (=) sign to tell the computer that on the right-hand side of the 19
equals sign is the value that we're assigning to that particular variable. 20
So you can essentially view creating variables similar to creating just a box, 21
right? 22
And you whack on a label which will be able to identify what's inside the box and you put some data inside 23
the box. And now you can store it somewhere on a shelf ready for retrieval. 24
And by looking at that label, you'll know what's contained inside. 25
So if you need the data that's inside, you simply find the right box with the right label. 26
Now, variables were made to be varied, 27
right? 28
=) 29
That's the whole point. 30
So we can simply remove the old value and put in a new one if we wanted to. 31
And now that label name "Angela" is associated with a completely different piece of data. In the resources. 32
for this lesson, you'll find a download link for the Swift Cheat Sheet that we've created for you. 33
And this is just a handy reference guide. 34
A lot of students like to print it out, to have it beside them when they're learning. 35
And it's a way for you to be able to quickly refer to the grammar of the Swift programming language. 36
So just as in English, we have our commas and full stops and they have meaning in the sentence. In Swift 37
and other programming languages, we have certain key words like the "var" key word, which is used for creating 38
variables or the equal (=) sign which is used for assigning a value to a variable. 39
And this is a handy guide that you can always flip back to when you're completing challenges and when 40
you're trying to write your own code. 41
So the way that we create a variable is we start out with the "var" keyword, then we give our variable 42
a name and then we assign it a value. 43
That's the theory of it. 44
But let's head into a playground and actually create some code from scratch. 45
So let's say that we had a variable called "a" and we gave it a number, let's say 5. And then let's 46
create another variable called "b" and let's give it a value of 8. 47
Now, if I decided to print these values, so print(a) and then print(b). Now notice in this case, I don't 48
have those quote marks around the a and b, because if I had that, then it would simply interpret this 49
as a command to simply print the letter or the string "a" and that's what you see here. 50
But if I actually want the value of my variable a, then I have to make sure that I'm putting it 51
in as a variable name a and b rather than anything with quotation marks in it. 52
I can make this a little bit fancier by adding some string interpolation. 53
If you remember "The value of a is" and then I can add that backslash and put my variable inside some 54
parentheses. And now I get printed, 55
"The value of a is 5". And I can do the same thing for b, "The value of b is \(b)" and let's just make sure 56
we're not missing that final quotation mark. 57
And now we get "The value of a is 5", "The value of b is 8". 58
So now that we've covered the theory of Swift variables, let's get a little bit more practice with 59
Swift programming through a coding challenge. 60
Now, as always, the challenges are. 61
completely optional, but solving them will help cement the concepts that we've covered and make you a 62
stronger programmer. Also figuring out and solving these little puzzles is going to be a lot of fun. 63
So what's the challenge? Without changing these lines or changing these lines, 64
can you swap the values of a and b? So that when these two lines execute, it will say the value of a 65
is 8 and the value of b is 5. 66
But here's the catch. 67
You can't type a single number. 68
So in your solution, you're not allowed to type a 5 or an 8. 69
You have to figure out how to switch these two without simply assigning it with a different number. 70
Because the easy way is to say "a" well now equals 8 and "b" now equals 5. 71
And I will get that a is equal to 8 and b is equal to 5. 72
And I've switched those two values around, but I have used a number. 73
So for this programming challenge, we're going to be using code to achieve this. 74
And you have all of the tools and knowledge to be able to do this. 75
But you might have to think a little bit outside of the box. 76
So this is a really, really common interview question that you will get as a programmer. 77
And it just tests you a little bit in terms of your lateral thinking and whether if you understand variables 78
well enough. I'll give you a few seconds to pause the video before I show you the model solution. 79
All right, ready? 80
Did you have a go at the assignment? And did you manage to get it right? 81
Now, the trick to solving this puzzle is to simply create another variable. 82
So let's go ahead and replace this line with our code. 83
I'm going to create another variable called "c", and I'm going to set c to equal the current value of 84
a. 85
So at this point, c is going to be equal to 5. 86
Now, next, we can set a=b 87
Now, at this point, a is now equal to 8. 88
And finally, we can set b to equal the value that's held inside c and so b now equals 5. 89
And within our three steps, if we run our code, you'll see that a is now 8 and b is now 5. 90
We've managed to switch these two values around in only three lines of code without needing to write 91
any numbers, such as a=8 or b=5. 92
I'll see you on the next lesson 93
where we'll learn more about Swift arrays.