0 1 00:00:00,740 --> 00:00:05,740 All right, guys, it's time for another episode of Swift Deep Dive. 1 2 00:00:05,740 --> 00:00:12,070 And in this lesson, we're going to cover a lot of the things that we've come across in the previous lessons 2 3 00:00:12,310 --> 00:00:14,420 when we were building out our app, 3 4 00:00:14,560 --> 00:00:17,530 and we're going to talk about them in a little bit more detail. 4 5 00:00:17,920 --> 00:00:21,710 So the first one I want to talk about is the Swift Constant. 5 6 00:00:21,940 --> 00:00:30,010 And the reason why I have a bronze elephant here is because recently I went to a bronze sword making 6 7 00:00:30,010 --> 00:00:37,300 workshop where you get to cast your own bronze sword from melted tin and copper. 7 8 00:00:37,300 --> 00:00:44,170 Now, the reason why I'm talking about bronze in a programming class is because similar to Constants, once 8 9 00:00:44,170 --> 00:00:48,580 you've cast the bronze, it's actually very hard to change it. 9 10 00:00:48,610 --> 00:00:56,080 It's not like iron which you can hammer and reshape. Bronze is really hard and it's just going to stay 10 11 00:00:56,530 --> 00:00:58,740 as the shape that you've cast it. 11 12 00:00:58,780 --> 00:01:03,100 And if you wanted to change it, then you would have to melt it down and recast it. 12 13 00:01:03,520 --> 00:01:09,160 So similarly, Constants once created, they don't change their values. 13 14 00:01:09,160 --> 00:01:14,380 So if we think about the last time when we had a Swift Deep Dive and we talked about this analogy of 14 15 00:01:14,410 --> 00:01:22,470 a variable being a box that contains data with a label on it so that you can refer to it later on, 15 16 00:01:22,630 --> 00:01:25,900 well, a Variable is kind of like a box with a lid on it. 16 17 00:01:25,900 --> 00:01:33,700 You can take out the value and put in a new value and change it as much as you wish. But a Constant is 17 18 00:01:33,700 --> 00:01:34,600 different. 18 19 00:01:34,600 --> 00:01:41,290 A Contant is kind of like a box which you taped shut and you can never ever change what's inside again. 19 20 00:01:41,710 --> 00:01:45,720 And creating a Constant is no different from creating a Variable. 20 21 00:01:45,760 --> 00:01:48,920 All we do is simply change the keyword. 21 22 00:01:48,970 --> 00:01:54,180 So instead of a 'var' for Variable, we create a 'let' for Constant. 22 23 00:01:54,190 --> 00:01:59,770 Now, the reason why it's called a 'let' is because Swift when it was designed, it was intended to be a 23 24 00:01:59,980 --> 00:02:03,370 very human readable programming language. 24 25 00:02:03,430 --> 00:02:09,950 So the idea is that you would say, well, let Angela = 0784236, et cetera. 25 26 00:02:10,060 --> 00:02:15,670 So it sounds like you're creating this thing and you're keeping it constant. 26 27 00:02:15,670 --> 00:02:22,630 And indeed if you go into a playground and if you create a variable "a" and you set it to equal 3, 27 28 00:02:22,780 --> 00:02:28,660 well, that's the initial stage. That's like building the box, putting the value in, and then you can tap 28 29 00:02:28,660 --> 00:02:34,930 into the value inside by simply using that reference, that "a," to use the value. 29 30 00:02:34,930 --> 00:02:41,830 In this case, I'm printing the value. But I can also vary the value, right? So I can say "a" now equals 5. 30 31 00:02:41,860 --> 00:02:46,330 Now, "a" has been reassigned to hold a different piece of data. 31 32 00:02:46,660 --> 00:02:52,580 And when I print "a" at this point, it's going to show 5 instead of 3. 32 33 00:02:52,600 --> 00:03:00,400 Now, if I had changed that keyword from 'var' to 'let' where I'm creating now a constant called "a," 33 34 00:03:00,400 --> 00:03:08,680 well, now I get an error because a card assign a new value to "a" because it's a Constant, 34 35 00:03:08,680 --> 00:03:13,320 and so my program crashes and I'm not allowed to continue. 35 36 00:03:13,330 --> 00:03:16,900 So why do we have Constants and Variables? 36 37 00:03:16,900 --> 00:03:18,370 What's the point? 37 38 00:03:18,910 --> 00:03:27,400 Well, part of it is to do with efficiency because if you create a constant, the computer knows that the 38 39 00:03:27,400 --> 00:03:35,350 piece of data that it has to hold is never going to change, the contents of that box will never change, 39 40 00:03:35,350 --> 00:03:38,440 so it can store the data more efficiently. 40 41 00:03:38,440 --> 00:03:43,960 For example, if I send you to the market, and some days, I asked you to buy five loaves of bread, 41 42 00:03:44,320 --> 00:03:46,930 but other times, you only need to buy one. 42 43 00:03:46,930 --> 00:03:53,950 Well, in this case, you might bring a large shopping basket with you, just so that you can adapt to this 43 44 00:03:54,010 --> 00:03:55,020 flexibility, 44 45 00:03:55,030 --> 00:03:55,710 right? 45 46 00:03:55,780 --> 00:03:58,590 One day you might need to buy a lots and put into the basket. 46 47 00:03:58,660 --> 00:04:02,010 And the other day, you might not fill your basket at all. 47 48 00:04:02,050 --> 00:04:09,250 So that's a Variable. But if you knew that every single day you needed to get two loaves of bread from 48 49 00:04:09,250 --> 00:04:14,760 the market every time, then you would bring a container that's perfectly sized for the job, 49 50 00:04:14,950 --> 00:04:17,090 so you don't have to carry any extra weight. 50 51 00:04:17,440 --> 00:04:25,270 So similarly, here, if I create a Variable "a" at the beginning, it might be storing a single number, right, 51 52 00:04:25,300 --> 00:04:31,390 which doesn't take a lot of space. But if at some point down the line, you need to just store a much larger 52 53 00:04:31,390 --> 00:04:39,700 number in that Variable, then that initial container, that box that gets created needs to be large enough 53 54 00:04:39,940 --> 00:04:43,240 to allow for this future flexibility. 54 55 00:04:43,240 --> 00:04:50,860 Now, had this been a let constant, however, well, then you only need to make a container as large as it 55 56 00:04:50,860 --> 00:04:53,590 needs to store this single value, 56 57 00:04:53,590 --> 00:04:55,150 so it's a lot more efficient. 57 58 00:04:55,210 --> 00:05:01,080 So that means that when you want to store a piece of data and give it a reference and you know that 58 59 00:05:01,080 --> 00:05:07,380 that data is never going to change, say, for example, you wanted to store the value for pi, 59 60 00:05:07,380 --> 00:05:07,990 right? 60 61 00:05:08,070 --> 00:05:16,400 And it was 3.14159, Well pi is not going to change between today and tomorrow. 61 62 00:05:16,590 --> 00:05:19,050 So you could create a Constant for it, 62 63 00:05:19,140 --> 00:05:19,820 right? 63 64 00:05:19,860 --> 00:05:25,980 Whereas if you had a user score, for example, well, that's constantly going to change. Every time they get 64 65 00:05:25,980 --> 00:05:32,130 a new point, then that score is going to be changed to a new value and you can't use a Constant. 65 66 00:05:32,130 --> 00:05:39,660 So by default, try to make everything a constant unless you need to change it. Now, the next thing I want 66 67 00:05:39,660 --> 00:05:46,920 to talk about are some of the most commonly used basic data types that you see in programming. 67 68 00:05:46,920 --> 00:05:51,100 Now, we've already seen a couple of data types in our programs. 68 69 00:05:51,180 --> 00:05:58,100 For example, whenever we print a piece of text to the debug console, we're creating a string. 69 70 00:05:58,320 --> 00:06:02,480 Now, a string is short for a string of characters. 70 71 00:06:02,550 --> 00:06:08,430 So just like you would have a string of pearls to create a necklace, you can have a string of characters 71 72 00:06:08,700 --> 00:06:10,820 to create a piece of text. 72 73 00:06:11,010 --> 00:06:17,520 Whereas we would normally call this text in programming, we often call it a string. And you can tell that 73 74 00:06:17,520 --> 00:06:18,930 something is a string 74 75 00:06:18,930 --> 00:06:25,830 because when we create it, we usually put two quotation marks around it to tell our code that this is 75 76 00:06:25,830 --> 00:06:28,330 just text and this is not code, 76 77 00:06:28,470 --> 00:06:36,160 so don't interpret it as code. The next data type that we saw in the last lesson was the "Int" which 77 78 00:06:36,160 --> 00:06:43,220 is short for integer. Now, an integer in mathematics refers to a whole number. 78 79 00:06:43,270 --> 00:06:46,120 It doesn't have any decimal places. 79 80 00:06:46,270 --> 00:06:51,430 So12 or 34, 1,239, 80 81 00:06:51,460 --> 00:06:56,320 these are all integers. And integers can also be negative as well. 81 82 00:06:56,320 --> 00:06:59,260 It's not limited to being a positive whole number. 82 83 00:06:59,290 --> 00:07:02,130 Now, what if you needed some decimal places? 83 84 00:07:02,140 --> 00:07:07,500 What if you were calculating something that needed a greater level of accuracy than a whole number? 84 85 00:07:07,510 --> 00:07:13,900 Well, you could use something called a float which is short for a floating-point number. 85 86 00:07:13,900 --> 00:07:17,800 And, of course, that floating point refers to the decimal point. 86 87 00:07:17,830 --> 00:07:26,320 Now, if you needed a greater number of decimal places to have greater accuracy so you're creating some 87 88 00:07:26,320 --> 00:07:33,040 sort of scientific calculator, and you need to give people, say, 600 decimal places, then you might 88 89 00:07:33,040 --> 00:07:36,660 want to consider using a data type called a double. 89 90 00:07:36,820 --> 00:07:43,510 The reason why it's called a double is because it has double the amount of accuracy of a float, 90 91 00:07:43,510 --> 00:07:47,400 so it can hold more numbers after the decimal place. 91 92 00:07:47,500 --> 00:07:54,430 And if we were storing pi which, of course, goes on until infinity, then we can have a much more precise 92 93 00:07:54,430 --> 00:07:59,650 calculation with our pi that has a double data type. 93 94 00:07:59,650 --> 00:08:06,910 Now, the final primitive or basic data type that all programming languages use is something called a 94 95 00:08:06,910 --> 00:08:15,660 bool which is short for a boolean, and a boolean only has two possible values: true or false. That's it. 95 96 00:08:15,760 --> 00:08:21,130 Because often in programming, you'll need to check if something is true, in which case, you should perform 96 97 00:08:21,220 --> 00:08:27,100 some sort of action, otherwise, or if it's false, then perform some other sort of action. 97 98 00:08:27,130 --> 00:08:33,000 So in the Swift Cheat Sheet, if you have a look at the red block here, we've got all of those data types 98 99 00:08:33,030 --> 00:08:39,400 I've talked about just now, plus some extra ones that we can learn about later on. And I've got some example 99 100 00:08:39,400 --> 00:08:45,700 values on the right, so that you can quickly remind yourself if you need to see what kind of values these 100 101 00:08:45,790 --> 00:08:47,700 data types refer to. 101 102 00:08:47,710 --> 00:08:49,330 That's a quick handy tip. 102 103 00:08:49,330 --> 00:08:54,550 Now, the final thing I want to talk about is randomization in Swift. In nature, 103 104 00:08:54,560 --> 00:09:01,840 there's a lot of randomness, the pattern of leaves falling from trees or the way that rain droplets fall 104 105 00:09:01,840 --> 00:09:03,010 on the water, 105 106 00:09:03,010 --> 00:09:05,860 these are all pretty much random events. 106 107 00:09:05,860 --> 00:09:13,690 Now, in a computer, though, where it's essentially just a bunch of switches, a bunch of ones and zeros ons 107 108 00:09:13,690 --> 00:09:14,320 and offs, 108 109 00:09:15,070 --> 00:09:20,360 it's a highly controlled environment where there really isn't much randomization. 109 110 00:09:20,380 --> 00:09:27,040 So how a random numbers generated from a computer? Say, previously, when we generated our random numbers, 110 111 00:09:27,430 --> 00:09:29,560 how does the computer even do that? 111 112 00:09:29,560 --> 00:09:35,740 Well, if you're curious about that, have a look at the course resources page where there's a link to a 112 113 00:09:35,740 --> 00:09:44,380 really good video on Khan Academy where Brit Cruise explains how random numbers or rather pseudo random 113 114 00:09:44,380 --> 00:09:48,570 numbers, the randomness that we get from computers, are generated. 114 115 00:09:48,880 --> 00:09:53,520 And it is a really, really fascinating video that gives you a lot more background on this topic 115 116 00:09:53,590 --> 00:10:00,550 if you're interested. Now, in our project, we saw that we could generate random whole numbers by using 116 117 00:10:00,640 --> 00:10:03,840 a functionality of the integer data type. 117 118 00:10:03,940 --> 00:10:09,850 So we wrote Int.random, and then we provided a lower and an upper bound. 118 119 00:10:09,850 --> 00:10:17,440 Now, in this case, what we get are random numbers anywhere between the lower and the upper bound and including 119 120 00:10:17,440 --> 00:10:18,550 the upper bound. 120 121 00:10:18,550 --> 00:10:28,030 So if we create a new constant here called randomNumber and we assign this reference to a new random 121 122 00:10:28,180 --> 00:10:36,340 integer between 1 and 3, and notice that there's three dots in between those numbers, you can separate 122 123 00:10:36,370 --> 00:10:36,840 these out 123 124 00:10:36,850 --> 00:10:39,040 if you find it easier to see this way. 124 125 00:10:39,310 --> 00:10:46,540 But this is what we call a closed range because when I hit print, my random number that we printed will 125 126 00:10:46,540 --> 00:10:50,280 be a whole number between 1 and 3, 126 127 00:10:50,410 --> 00:10:52,700 but inclusive of 1 and 3. 127 128 00:10:52,710 --> 00:10:55,340 So I might get 1, I might get 3, I might I get 2. 128 129 00:10:55,720 --> 00:11:03,880 So if I stop my playground and rerun my playground, then you can see that I'll get different random numbers, 129 130 00:11:04,210 --> 00:11:09,040 but I've already traversed all three numbers: 1, 2, and 3. 130 131 00:11:09,070 --> 00:11:18,250 So all of the numbers in this close range are possible. Now, just now you saw me use a closed range and 131 132 00:11:18,310 --> 00:11:25,510 that refers to something called a range operator in Swift. For example, the three darts that we used would 132 133 00:11:25,510 --> 00:11:34,180 be known as a closed range operator where the range is inclusive of the upper bound. But Swift also has 133 134 00:11:34,270 --> 00:11:42,940 a half open range operator and this is done by using two dots and a less than sign. 134 135 00:11:43,210 --> 00:11:50,850 So this defines a range between the lower and the upper bound but not including the upper bound. 135 136 00:11:50,860 --> 00:11:59,080 So if I change that last dot to a less than sign, then you can see that I get one and two occasionally, 136 137 00:11:59,260 --> 00:12:06,810 but I will never get three. If I wanted a different type of number to be generated, say, if I wanted a 137 138 00:12:07,170 --> 00:12:14,310 random floating-point number, then I can simply change the data type at the beginning to a float. We can 138 139 00:12:14,310 --> 00:12:21,080 change that Int to a float, and we will start generating random numbers with decimal places. 139 140 00:12:21,210 --> 00:12:28,590 And in this case, because it's a close range, it could include 1.0000 all the way 140 141 00:12:28,680 --> 00:12:31,970 up until 3.0000. 141 142 00:12:32,040 --> 00:12:38,460 But if we change this to a half-open range, then you can only get up to 2.999 142 143 00:12:38,460 --> 00:12:43,550 recurring, and you will never get 3.0000. 143 144 00:12:43,560 --> 00:12:48,990 So just by changing that data type at the beginning, you can determine what type of random number you 144 145 00:12:48,990 --> 00:12:49,770 get. 145 146 00:12:49,830 --> 00:12:54,720 And this works for booleans as well. But, of course, booleans can only be true or false, 146 147 00:12:54,750 --> 00:12:58,140 so there's a range in this case. 147 148 00:12:58,140 --> 00:13:05,100 Now, we saw in the last lesson that we can get hold of a random element from an Array by asking for a 148 149 00:13:05,100 --> 00:13:07,620 random element from our Array. 149 150 00:13:07,920 --> 00:13:15,270 But you can also use a shuffle method, so that the items in the array change their positions in the Array 150 151 00:13:15,540 --> 00:13:16,830 completely randomly. 151 152 00:13:16,920 --> 00:13:21,170 And we're going to be using this in a later lesson as well. 152 153 00:13:21,270 --> 00:13:27,780 Now, that you're well versed with randomization and Variables and Constants and Arrays, it's time for 153 154 00:13:27,870 --> 00:13:33,690 another challenge and the challenge is to create a password generator. 154 155 00:13:33,690 --> 00:13:41,640 So here we've created an Array which has all of the letters in the alphabets and each of them are stored 155 156 00:13:41,700 --> 00:13:42,570 as a string, 156 157 00:13:42,570 --> 00:13:48,210 so with quotation marks around them. And there's a total of 26 letters in the alphabet. 157 158 00:13:48,330 --> 00:13:57,030 Now, I want you to write some code to randomly pick six letters out of the alphabet to create a random 158 159 00:13:57,150 --> 00:13:59,520 six character password. 159 160 00:13:59,520 --> 00:14:05,520 Now, it's important to note that you can combine different characters together by simply using the plus 160 161 00:14:05,520 --> 00:14:05,980 sign. 161 162 00:14:06,030 --> 00:14:13,470 So here if I create a constant code password which is made up of the character "a," plus the character 162 163 00:14:13,470 --> 00:14:14,260 "b," 163 164 00:14:14,400 --> 00:14:20,880 then when I print this password, then you'll see what prints is "a," "b." 164 165 00:14:20,970 --> 00:14:29,240 The strings are combined together with no space in between. And in programming, we call this concatenation. 165 166 00:14:29,250 --> 00:14:32,060 So just putting them right next to each other 166 167 00:14:32,400 --> 00:14:34,350 and you do that using the plus sign. 167 168 00:14:34,380 --> 00:14:43,350 So coming back to our challenge, can you create this six-character password which is a random bunch of 168 169 00:14:43,350 --> 00:14:45,930 characters from our alphabet array? 169 170 00:14:46,080 --> 00:14:52,560 Once you've completed this challenge, then it's time to head over to the next module and create an app 170 171 00:14:52,740 --> 00:14:53,760 all by yourself 171 172 00:14:53,760 --> 00:14:55,020 based on what you've learned.