Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Constants are similar to variables as they too are used to store information. However, as their name states they remain constant which means their value cannot be changed.
-
0:00
Congratulations, now we know about a fundamental building block of programming,
-
0:05
variables.
-
0:07
As we just discussed, a variable is a container for some data.
-
0:11
And as the name suggests, we can change the contents of the variable.
-
0:15
While this seems like a flexible option and a big advantage, it really isn't.
-
0:20
Let's look at an example.
-
0:22
In real life, let's say I'm out grocery shopping with my son.
-
0:26
I have my shopping basket which has one item in it, some apples.
-
0:30
I tell him to hold on to it but
-
0:32
not to put anything else in there, because kids love to get random things.
-
0:37
A few minutes later I come back and ask for the shopping basket.
-
0:41
But instead of seeing just the apples in there,
-
0:43
there's like a thousand boxes of candy.
-
0:46
Now I, like anyone else, would notice that this basket contains something different
-
0:51
from what I started with.
-
0:53
Like we discussed earlier, computers aren't that smart.
-
0:57
If you assign a different string to the same variable,
-
1:00
it's not smart enough to know that the string changed.
-
1:04
What if we wanted to store values that we don't want to change?
-
1:08
Things like the speed of light, or
-
1:09
the acceleration of the Earth's gravity, or a shopping basket of apples.
-
1:14
We can do this with constants in Swift.
-
1:18
We know that a variable can be thought of as a box with some contents, or
-
1:22
data inside.
-
1:24
A constant is similar, except that when you add data to the box,
-
1:28
we seal it off with a lock and the contents of the box can never change.
-
1:32
We can still see what's inside the box, we just can't reach it and change it anymore.
-
1:37
We can still use it.
-
1:39
Let's look at this in code.
-
1:41
Now, before we write any code,
-
1:43
I'm going to add a comment to keep this playground somewhat organized.
-
1:47
So we'll set a new section here.
-
1:50
We'll say constants, because that's what we're about to learn.
-
1:54
Now at the top, this comment doesn't really mean much, so we'll get rid of it,
-
1:59
and we'll say variables.
-
2:04
Just like we had a special keyword to create a variable,
-
2:07
we have another keyword to create a constant, let.
-
2:11
Now after we use let, the rest of the syntax is exactly the same.
-
2:16
In the code challenge, I asked you to create a variable named language, and
-
2:20
assign the string, Swift.
-
2:22
So over here, let's do that with a constant.
-
2:25
So I'll start with the keyword let, followed by the name of the constant.
-
2:30
So here we're going to say language, and to that we'll assign the string.
-
2:35
Remember we use the assignment operator and
-
2:38
then the actual string, which here is Swift.
-
2:42
So far so good.
-
2:43
The second part of the challenge asked you to then assign the string Objective-C,
-
2:48
to the same variable.
-
2:50
Of course let's try that with a constant.
-
2:52
So remember to change the value of an existing variable,
-
2:57
we just use the name, right?
-
2:58
We didn't use a keyword var.
-
3:00
So we'll do that here as well, so we'll start with language.
-
3:04
And now we'll try to change the value assigned to it by assigning a new string,
-
3:09
Objective-C.
-
3:12
Before you're even done typing, you should see an error.
-
3:15
Now remember that we can click on the red symbol
-
3:17
in the gutter over here to see what the error is.
-
3:21
You might notice that the error symbol isn't that stop sign looking thing
-
3:25
anymore.
-
3:26
But a red circle with a white dot.
-
3:29
This symbol indicates that you have an error,
-
3:32
and that Xcode has a recommendation on how to fix it.
-
3:36
So if we click on it you'll see that the error says,
-
3:39
Cannot assign to value because language is a let constant.
-
3:43
Over here it offers a fix.
-
3:46
It says, Change let two var to make it mutable.
-
3:49
Meaning change it to a variable so that we can change it.
-
3:53
Now, we don't want to do that, so for now let's just get rid of this line of code.
-
3:58
Constants don't seem so different from variables when you write it out, but
-
4:02
under the hood it makes a pretty big difference.
-
4:05
Since we're just starting out,
-
4:06
it's hard to understand why we don't want data to change.
-
4:10
But trust me, we really don't want it to change.
-
4:13
Once we start building apps together, the reasoning will become a lot more apparent.
-
4:18
There is a programmer term for this, and it's worth getting familiar with.
-
4:22
A variable containing a piece of data is known as a mutable type.
-
4:28
So if we do, var str, and we assign the string, Hi, there to it.
-
4:33
Because the variable holds onto a string that we can change at any time,
-
4:38
we call it a mutable string.
-
4:40
In contrast, a constant containing data cannot change, and
-
4:45
is known as an immutable type.
-
4:47
Since the string is a constant,
-
4:49
meaning the value assigned to it is constant, we cannot change this value.
-
4:54
So we call it an immutable string.
-
4:56
A bit of history before we end this topic.
-
4:59
A lot of people ask why the term let is used.
-
5:02
Var seems like a logical choice because it's short for variable, but
-
5:06
what about let?
-
5:08
Let is used in many different programming languages, so the origins are somewhat
-
5:12
murky, but the general consensus is that it stems from mathematical usage.
-
5:18
In math it's quite common to say let x denote some value.
-
5:22
That usage continued on in computer science and
-
5:25
ended up being used in different languages, although with varying meaning.
-
5:30
I also want to take a quick second to talk about Xcode again before we conclude
-
5:34
this video.
-
5:35
When writing code, as we've been doing, every time you hit enter or
-
5:39
go to the next line.
-
5:41
You may have noticed that the playground that you're working in evaluated your code
-
5:45
and then displayed some results, either in the results area here, or
-
5:50
in this area below, that's called the console.
-
5:53
Now if you don't have this area visible, you can click on this icon,
-
5:57
in the second set, it's the middle icon, to make it appear and disappear.
-
6:01
And this isn't how coding typically works.
-
6:05
Code isn't always running all the time, and
-
6:07
results aren't displayed in a results area as you see here.
-
6:11
This is because in Xcode playground, is a specialized
-
6:15
interactive environment that reads, evaluates, and prints our code in a loop.
-
6:21
These types of environments are called REPLs.
-
6:24
R-E-P-L for read, evaluate, print, loop.
-
6:28
Anyway, it's nothing you need to memorize.
-
6:30
But it's important to know that your playing ground is doing
-
6:33
some work on your behalf.
-
6:35
If for some reason while you're working through this code, your code is not
-
6:39
evaluated, because well, Xcode can tend to be baggy, as all software is.
-
6:44
You can hit the play button at the very bottom, so
-
6:47
right here in my window, which is attached to this console bar.
-
6:51
You can hit this play button to get it to manually execute the playground again.
-
6:56
Or worst case, just quit Xcode and start again if things stop working.
-
7:02
Before we round up our discussion on variables and constants,
-
7:05
let's get in a bit more practice.
You need to sign up for Treehouse in order to download course files.
Sign up