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