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