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