0 1 00:00:00,710 --> 00:00:04,190 So our app now at least has some functionality 1 2 00:00:04,190 --> 00:00:08,860 and when we pressed the Roll button, things actually happen on screen. 2 3 00:00:08,870 --> 00:00:13,590 Well, at least until it crashes when we run out of images to display. 3 4 00:00:14,150 --> 00:00:16,550 So there's a couple of things I want to do here. 4 5 00:00:16,550 --> 00:00:22,460 First, I want to simplify the code a little bit because we no longer need anything to happen when the 5 6 00:00:22,460 --> 00:00:29,390 view loads up, then we no longer have a need for this block of code. So we can simply go ahead and delete 6 7 00:00:29,390 --> 00:00:30,310 it. 7 8 00:00:30,350 --> 00:00:37,460 Now, the next thing I want to show you is that while we have this very clear way of writing leftDiceNumber 8 9 00:00:37,460 --> 00:00:46,070 equals the current value of leftDiceNumber + 1 is actually a far more efficient way of writing 9 10 00:00:46,070 --> 00:00:46,960 this. 10 11 00:00:47,030 --> 00:00:53,630 And because in programming, we often need to increase the value of variables by 1 or by 2, 11 12 00:00:53,630 --> 00:00:57,380 this is why there's prebuilt ways of making this easier. 12 13 00:00:57,410 --> 00:01:05,030 So in Swift, we could simply write += 1, and that will take the current value of leftDiceNumber 13 14 00:01:05,030 --> 00:01:11,980 add 1 to it, and then set it to the value of that Variable. 14 15 00:01:12,350 --> 00:01:18,140 But if you look at this straight away without the context, then it can be a little bit confusing as to 15 16 00:01:18,140 --> 00:01:19,310 what's going on. 16 17 00:01:19,340 --> 00:01:26,210 So I'll leave you to be the judge of which version you prefer. And remember that it's not just plus that 17 18 00:01:26,210 --> 00:01:34,490 you can do this with, you can do minus or multiply or divide, and it will basically do the calculation 18 19 00:01:34,580 --> 00:01:40,910 as if you're taking the current value dividing it by the value after equal sign, and then assigning 19 20 00:01:40,910 --> 00:01:46,920 it as the value inside the variable. I'm going to leave that as a += 1 20 21 00:01:47,000 --> 00:01:54,770 and I'm going to change this to -= 1. And if you run your app, you'll see that it works exactly 21 22 00:01:54,770 --> 00:01:56,110 the same way as before. 22 23 00:01:56,240 --> 00:02:01,640 Nothing has changed other than the fact that we've made our code a lot shorter. 23 24 00:02:01,750 --> 00:02:03,760 Now, there's gains and other places as well 24 25 00:02:03,800 --> 00:02:06,470 and we'll talk about them when we come across them. 25 26 00:02:06,470 --> 00:02:14,750 But the main goal right now is to make our app not crash when it gets to the end of the Array and the 26 27 00:02:14,750 --> 00:02:22,280 other thing to think about is that it's not really a dice app if it just goes through all the numbers 27 28 00:02:22,280 --> 00:02:23,560 in sequence, right? 28 29 00:02:23,570 --> 00:02:25,580 That's not random. 29 30 00:02:25,610 --> 00:02:29,580 So how do we get some random numbers showing up instead? 30 31 00:02:29,660 --> 00:02:35,710 Well, in swift you can generate a random number by simply writing Int. 31 32 00:02:35,720 --> 00:02:42,920 So this is the data type that refers to a integer which is also known as a whole number, 32 33 00:02:42,920 --> 00:02:44,990 so no decimal places. 33 34 00:02:45,620 --> 00:02:54,890 And then if we write a .random, then we can tap into a range of numbers. So we can say give us a random 34 35 00:02:55,310 --> 00:03:00,690 whole number from 1 to 10. 35 36 00:03:00,860 --> 00:03:03,010 And that's what that three dots do. 36 37 00:03:03,020 --> 00:03:07,280 It's specifying a range from 1 until 10. 37 38 00:03:07,280 --> 00:03:15,770 And when this line of code runs, it will look at the range and generate a random number between 1 and 38 39 00:03:15,770 --> 00:03:16,310 10. 39 40 00:03:16,310 --> 00:03:18,980 So inclusive of both 1 and 10. 40 41 00:03:18,980 --> 00:03:26,910 Now, if I go ahead and simply wrap a print statement around that and hit run, and when I press the Roll 41 42 00:03:26,910 --> 00:03:34,110 button, you can see that we get a bunch of random numbers being printed. Well, at least until our app crashes 42 43 00:03:34,260 --> 00:03:36,200 because of this line again. 43 44 00:03:36,600 --> 00:03:39,160 So let's put two and two together. 44 45 00:03:39,360 --> 00:03:48,270 What we need is a random number here to be able to pick out a random image from our Array. 45 46 00:03:48,300 --> 00:03:53,430 So we have six array images and they start counting from zero. 46 47 00:03:53,460 --> 00:03:57,810 So this one is at position zero until position 5. 47 48 00:03:57,810 --> 00:04:04,640 So we basically need this to be a random number between 0 and 5, 48 49 00:04:04,650 --> 00:04:04,950 right? 49 50 00:04:05,490 --> 00:04:09,890 So maybe at one point when we press the Roll button, it'll generate 4. 50 51 00:04:10,020 --> 00:04:13,680 But the next time, it'll be 2, and then the next time, it'll be zero. 51 52 00:04:14,040 --> 00:04:15,940 But it has to be random. 52 53 00:04:16,050 --> 00:04:22,920 So let's transplant this code here inside the print statement which we already know can generate us 53 54 00:04:23,010 --> 00:04:26,480 random numbers between 1 and 10, 54 55 00:04:26,490 --> 00:04:31,940 but let's replace that range and make it between 0 and 5 55 56 00:04:31,980 --> 00:04:35,950 and I'm going to do the same for the bottom as well. 56 57 00:04:36,090 --> 00:04:43,510 I'm going to delete that print statement and let's run our code again. Now, if I press the Roll button, 57 58 00:04:43,870 --> 00:04:52,240 you can see that I'm getting completely random numbers showing up. And there is no end to this app anymore 58 59 00:04:52,510 --> 00:04:59,710 because it's just going to stay within the limits of our range here that we've specified between zero 59 60 00:04:59,710 --> 00:05:00,490 and 5. 60 61 00:05:00,490 --> 00:05:06,530 It's never going to go to 6 or 7 or 8 which does not correspond to an image in our Array. 61 62 00:05:06,610 --> 00:05:10,150 We can no longer crash our app which is wonderful. 62 63 00:05:10,150 --> 00:05:14,340 So we've made our app behave more like a real dice. 63 64 00:05:14,410 --> 00:05:21,820 And in addition, we can now delete this leftDiceNumber and rightDiceNumber both where we are using 64 65 00:05:21,820 --> 00:05:23,920 it as well as where we're creating it. 65 66 00:05:24,400 --> 00:05:27,220 So our code is now even simpler. 66 67 00:05:27,220 --> 00:05:33,490 Now, this is what we call refactoring in programming where we take our code and try to make it simpler 67 68 00:05:33,910 --> 00:05:41,400 and make sure that we don't have any code that is repeating or unnecessary. We can take this even further 68 69 00:05:41,460 --> 00:05:48,840 because look right here, we have that same Array that's being used in both places. 69 70 00:05:48,840 --> 00:05:55,830 So why don't we, instead, just create a reference to it, give it a name and use the name every time, instead 70 71 00:05:55,830 --> 00:06:02,910 of having to create this Array. And even though Xcode makes this display really nicely and show us the 71 72 00:06:02,910 --> 00:06:09,720 images, as soon as there's some issue in your code, let's say, we commented out this code, you can see what's 72 73 00:06:09,720 --> 00:06:12,300 behind the scenes is actually quite complex. 73 74 00:06:12,300 --> 00:06:17,470 There's something called an imageLiteral being created from a resourceName, 74 75 00:06:17,580 --> 00:06:23,970 and this makes our code quite long and quite wordy. So it'd be great if we didn't have this duplication. 75 76 00:06:24,180 --> 00:06:32,340 So what if we could create a variable that holds this Array as the Variable's value and then we can refer 76 77 00:06:32,340 --> 00:06:37,550 to that Variable when we need to use this Array. Just above these two lines, 77 78 00:06:37,560 --> 00:06:45,690 I'm going to create a new Variable which I'll call diceArray and I'm going to set it to equal this 78 79 00:06:45,810 --> 00:06:46,910 array right here, 79 80 00:06:46,920 --> 00:06:52,980 so I'm making sure I'm copying in all the square brackets and I'm going to paste it in here. 80 81 00:06:52,980 --> 00:07:01,350 Now, in Swift, it's really important to make sure that you have consistent or symmetric amount of space 81 82 00:07:01,710 --> 00:07:03,900 on either side of the equal sign. 82 83 00:07:04,290 --> 00:07:11,610 So for example, you could have no space, either side of the equal sign, or you could have one space. But 83 84 00:07:11,610 --> 00:07:17,400 try to avoid it where you have an asymmetric situation like this because you'll get an error from 84 85 00:07:17,400 --> 00:07:18,090 Xcode. 85 86 00:07:18,090 --> 00:07:21,720 Now, when you see a warning or a error, 86 87 00:07:21,900 --> 00:07:27,930 so the yellow triangles are the warnings which says, "Hey, I think there might be something wrong, but you 87 88 00:07:27,930 --> 00:07:28,650 might know better, 88 89 00:07:28,650 --> 00:07:33,300 so you can continue," or the red stop signs say, "Stop. 89 90 00:07:33,300 --> 00:07:37,100 There is an error and you must fix it if you want to run this app." 90 91 00:07:37,380 --> 00:07:44,430 And when these symbols have a little white dot in it, it means that Xcode thinks that it knows the solution 91 92 00:07:44,430 --> 00:07:45,670 to this problem. 92 93 00:07:45,690 --> 00:07:53,340 So if you click on it, you can see a Fix button, and you can see what it's going to try and do when you 93 94 00:07:53,340 --> 00:07:54,630 click on this Fix button. 94 95 00:07:54,990 --> 00:07:59,120 So in this case, it's going to insert a space after the equals sign. 95 96 00:07:59,190 --> 00:08:04,530 Let's click Fix and it automatically fixes that for us. 96 97 00:08:04,590 --> 00:08:10,350 Now, it's really important that when you click on the Fix button, you know exactly what it's trying to 97 98 00:08:10,350 --> 00:08:12,650 do and why it's doing it, 98 99 00:08:12,840 --> 00:08:19,740 so you understand what's going on before you just let Xcode fix everything because Xcode is still 99 100 00:08:19,740 --> 00:08:21,690 quite far from Skynet. 100 101 00:08:21,690 --> 00:08:25,230 It thinks it's very clever, but it's actually not. 101 102 00:08:25,500 --> 00:08:31,280 So there are many cases where it thinks it knows the solution, but actually it just messes up your code. 102 103 00:08:31,380 --> 00:08:36,780 You are the human programmer. You are the one telling the computer what to do. Make sure that you know 103 104 00:08:36,780 --> 00:08:40,100 exactly what Xcode is trying to fix before you click on it. 104 105 00:08:41,100 --> 00:08:46,650 Now, that we've got this dice array here, I'm going to ignore this warning for now and then we'll come 105 106 00:08:46,650 --> 00:08:48,090 back to it a little bit later on. 106 107 00:08:48,600 --> 00:08:57,610 But the point is we now have a reference to our Array and we can reduce the duplication right here. 107 108 00:08:57,690 --> 00:09:03,440 So instead of actually providing the Array, we can simply refer to the Array, 108 109 00:09:03,480 --> 00:09:10,410 and if I type in its name down here, then when this line of code runs, it's gonna go and look for 109 110 00:09:10,410 --> 00:09:12,880 this diceArray which lives right here, 110 111 00:09:13,200 --> 00:09:20,170 and then it's going to try and pull out a random item from it between 0 and 5. 111 112 00:09:20,220 --> 00:09:26,850 Now on the next line, I can do exactly the same thing. So use the reference to diceArray, 112 113 00:09:27,120 --> 00:09:32,100 and if we run our app right now, you'll see that nothing has changed. 113 114 00:09:32,100 --> 00:09:39,810 We still have our dice rolling functionality and it rolls any random dice between 1 and 6. 114 115 00:09:39,810 --> 00:09:44,000 Now, let's come back and address this warning from Xcode. 115 116 00:09:44,010 --> 00:09:46,920 It says, the variable dice array, 116 117 00:09:46,920 --> 00:09:52,140 this one, was never mutated consider changing to a 'let' 117 118 00:09:52,140 --> 00:09:53,220 constant. 118 119 00:09:53,250 --> 00:09:55,530 What is this trying to tell us? 119 120 00:09:55,530 --> 00:10:01,500 Well, here we've created a variable which is created using the var keyword 120 121 00:10:01,680 --> 00:10:08,310 and we've basically given this collection of items a name, so that we can refer to it later on. 121 122 00:10:08,850 --> 00:10:14,790 But given that it's a variable, surely, we would want to vary it at some point, 122 123 00:10:14,790 --> 00:10:15,390 right? 123 124 00:10:15,450 --> 00:10:20,770 But at no point in our code do we ever change the value of dice array, 124 125 00:10:21,240 --> 00:10:22,920 it's not like right at the end, 125 126 00:10:22,920 --> 00:10:30,010 we tap into the diceArray and we set it equal to a different set of image literals. 126 127 00:10:30,090 --> 00:10:39,900 So maybe the diceArray is now composed of the logo and the background image. 127 128 00:10:39,900 --> 00:10:48,900 In this case, we've created the Variable and then, subsequently, we vary the contents of that Variable, 128 129 00:10:48,930 --> 00:10:52,240 so we've changed the values that's held inside. 129 130 00:10:52,680 --> 00:10:54,940 Well, this is what a Variable is used for, 130 131 00:10:55,020 --> 00:11:01,680 and it's really common. Well, say, if you had a score, right, where the user starts out with a score of 131 132 00:11:01,680 --> 00:11:02,560 zero, 132 133 00:11:02,640 --> 00:11:07,420 but then, later on, we add one to it, so that we increase their score. 133 134 00:11:07,440 --> 00:11:15,090 Well, this is where we're varying the Variable. But in our case, we don't actually need to vary the 134 135 00:11:15,090 --> 00:11:18,090 diceArray because that's all the images we have. 135 136 00:11:18,090 --> 00:11:23,940 We don't have any other images. So why do we need to change it? 136 137 00:11:23,940 --> 00:11:24,600 Well, we don't. 137 138 00:11:24,840 --> 00:11:29,700 So that's why Xcode is telling us consider changing it to a 'let' 138 139 00:11:29,700 --> 00:11:30,680 constant. 139 140 00:11:30,780 --> 00:11:34,940 And, again, this warning here has a white circle in it. 140 141 00:11:35,160 --> 00:11:38,390 So it means Xcode thinks it knows how to fix this. 141 142 00:11:38,580 --> 00:11:41,140 Let's click on it and see its suggestion. 142 143 00:11:41,160 --> 00:11:48,750 It says, "Why don't you replace the 'var' keyword with a 'let' keyword to turn it into a constant.?" 143 144 00:11:48,780 --> 00:11:50,280 So let's click on Fix. 144 145 00:11:50,820 --> 00:11:53,440 And now all our warnings go away. 145 146 00:11:53,490 --> 00:11:59,770 And if we run up again, it will work exactly the same as before. 146 147 00:11:59,940 --> 00:12:05,530 But all that we've done is instead of allowing this to be changed, 147 148 00:12:05,610 --> 00:12:07,320 this is now a constant. 148 149 00:12:07,320 --> 00:12:15,240 So if I decided that diceArray was going to have some other images inside, I don't' know, let's say, if we deleted 149 150 00:12:15,240 --> 00:12:19,200 some of these values, then you'll see that it'll actually give us an error. 150 151 00:12:19,200 --> 00:12:23,980 You can't assign to this value. diceArray is a constant. 151 152 00:12:24,090 --> 00:12:31,260 And it tells us that if you do want to change this Array and you want to change its values, well, then 152 153 00:12:31,260 --> 00:12:36,420 you have to change that 'let' back to a 'var,' and that's what it's automatic fix does. 153 154 00:12:36,450 --> 00:12:37,860 So we don't want that. 154 155 00:12:37,890 --> 00:12:43,650 We're never going to change the Array, so we can keep it as a 'let' constant. 155 156 00:12:43,650 --> 00:12:53,190 We've seen that we can generate a random whole number between the range of 0 to 5 and this is a really 156 157 00:12:53,190 --> 00:12:55,070 heavily used bit of code. 157 158 00:12:55,200 --> 00:13:00,720 Whenever you're programming anything that needs some randomness, say, for example, a game, 158 159 00:13:00,720 --> 00:13:09,090 but in our case, if we just wanted a random item out of our diceArray, there's actually an even simpler 159 160 00:13:09,090 --> 00:13:10,170 way of doing it. 160 161 00:13:10,260 --> 00:13:20,010 We can delete this and simply write .randomElement and that will do exactly the same as this. 161 162 00:13:20,010 --> 00:13:27,450 It will look through this Array, see how many items there are inside, and then give us a random element. 162 163 00:13:27,480 --> 00:13:35,040 So if you wanted the ultimate succinct code, then it would probably look something like this where you're 163 164 00:13:35,160 --> 00:13:42,420 picking a random element for each of these diceImageViews. But if you find it easier to see what's 164 165 00:13:42,420 --> 00:13:49,230 going on and the practice using this random generator, then you might want to keep your code like this. 165 166 00:13:49,230 --> 00:13:55,830 The choice is yours. But I recommend that at this point, you take a moment to write some comments in the 166 167 00:13:55,830 --> 00:14:03,090 code to tell your future self what each of these lines of code that you've written does, so that when 167 168 00:14:03,090 --> 00:14:08,520 you come back to it, when you review it, you'll be able to understand quickly at a glance. 168 169 00:14:08,520 --> 00:14:13,860 So pause a video and have a think about some of the new things you've learned in this lesson and write 169 170 00:14:13,860 --> 00:14:17,340 some notes on those things. In the next lesson, 170 171 00:14:17,370 --> 00:14:23,850 we're going to be doing another deep dive into randomization in Swift, as well as the range operator 171 172 00:14:23,880 --> 00:14:24,790 in Swift, 172 173 00:14:25,020 --> 00:14:30,260 and look at the difference between the 'let' and the 'var' and when you would use each. 173 174 00:14:30,270 --> 00:14:34,280 So if you already know about all those topics, then feel free to skip ahead. 174 175 00:14:34,440 --> 00:14:37,740 But if you want to learn more, then have a look at the next video.