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
Now that we conceptually know what an optional is, we look at why it even exists to begin with and the kinds of benefits we offer. We also take a look at one way (THE WRONG WAY) to work with optional values.
-
0:00
So what use does an optional offer us?
-
0:03
Many languages don't have a feature to indicate that a type could be nil or
-
0:07
NULL at some point.
-
0:09
Instead, we have to remember to check in many places.
-
0:12
For example, let's take a look at some Objective-C code.
-
0:16
In Objective-C, like a lot of languages, anything can be set to nil.
-
0:21
This seems useful at first if you don't have a value for some type.
-
0:25
You can easily set it to nil.
-
0:27
But this means we have to constantly be checking if that something is nil.
-
0:32
Think of it this way.
-
0:33
We talked about constants and variables being boxes, right?
-
0:37
Imagine that when opening the box, if the value inside is nil and
-
0:40
we simply open the box, the box just explodes.
-
0:43
Think of a box exploding as equivalent to your app crashing.
-
0:46
>> [SOUND] >> Objective-C is kind of like that.
-
0:49
If you're not expecting nil and you open the box, you get an app crash.
-
0:53
That is, your box is going to explode.
-
0:55
>> [SOUND] [COUGH] >> As you saw from the code earlier,
-
0:57
let's put it back up on the screen,
-
1:00
there's nothing to indicate that this type could contain a nil value.
-
1:04
When it holds a string, the type is NSString.
-
1:07
And when it holds nil, the type is still NSString.
-
1:10
So the chances of our box exploding is pretty high.
-
1:14
Going back to our example,
-
1:15
let's say that we have a special box opener that diffuses this nil bomb.
-
1:20
If we open the box with this special opener and it's nil, we're okay.
-
1:23
We can continue working.
-
1:25
But using this special opener is cumbersome, and
-
1:28
we need to manually remember it, so
-
1:30
developers get lazy and forget to use it sometimes, and our boxes explode.
-
1:34
>> [SOUND] [COUGH] >> Swift,
-
1:35
on the other hand, indicates that a box can be nil.
-
1:39
And if the box is nil, you can only open it with this safe opener.
-
1:43
You could try to open it regularly, but it won't open it.
-
1:46
It forces you to use this safe option.
-
1:50
Here we have an instance of Person that I've created with my first and last name.
-
1:55
Let's add a method to the struct,
-
1:57
an instance method, that lets us return a person's full name.
-
2:01
So we'll call this method fullName, and it returns a String.
-
2:07
Inside, we can just concatenate the three properties to form a fullName
-
2:11
string and return it.
-
2:13
So we'll say return firstName + " "
-
2:20
+ middleName + " " + lastName.
-
2:26
Now, the second we do this, we get a compiler error.
-
2:29
Swift won't let us do this, and this is because middleName
-
2:33
right here is an optional string and it may not contain a value.
-
2:37
We can't concatenate a string that could possibly not be a string, right?
-
2:42
How do you add nil to this string right here?
-
2:45
This highlights an important point.
-
2:47
A string and an optional string are not the same type.
-
2:52
The optional string can be nil, but a regular string cannot.
-
2:56
In a language without the type safety, an optional feature that Swift has,
-
3:00
this code would work fine.
-
3:02
It would compile perfectly.
-
3:03
But if I called it on my name, it would explode, it would crash.
-
3:08
To work with the value, we need to unwrap it.
-
3:12
If you think of an optional as this special protection around a potentially
-
3:16
nil value, unwrapping means peeling that layer of protection and
-
3:20
using the value inside if it exists.
-
3:23
There are several ways we can unwrap an optional.
-
3:25
And for all these different ways, control flow is our friend.
-
3:29
So let's go back in here, and we'll get rid of this.
-
3:33
We'll start with a simple if statement and check if middleName is equal to nil.
-
3:38
So we'll say if middleName == nil.
-
3:44
Then we're only going to return the first and last name.
-
3:48
So return firstName + " " + lastName.
-
3:55
Now, I know I can use string interpolation so that I don't have to concatenate
-
3:59
a string with whitespace, but there's a reason for that.
-
4:02
Now, if the middle name is not nil, we can return the full name.
-
4:06
So we'll return firstName + " "
-
4:11
+ middleName + " " + lastName.
-
4:17
Well, this doesn't work, obviously,
-
4:19
because middleName is still wrapped inside this optional box.
-
4:22
We need to get that value out.
-
4:24
So again, we're going to add one character after middleName, with no spaces,
-
4:28
add an exclamation point.
-
4:30
And just like that, it works.
-
4:32
This actually compiles.
-
4:34
So now, if I type me.fullName, and we call the instance method,
-
4:38
I should get my full name back without a middle name, as you can see right here.
-
4:43
If I were to add some random middle name in here, that'll also work.
-
4:48
So this is the first way that you can unwrap a value
-
4:51
using this exclamation point.
-
4:54
But now that I've shown you this,
-
4:55
I want you to immediately erase it from your mind forever.
-
4:59
Never ever, ever, ever do this.
-
5:01
Using an exclamation mark like this is called force unwrapping.
-
5:06
What this indicates is I know that this is an optional, and
-
5:10
I know that this definitely has a value, so go ahead and rip it out and use it.
-
5:15
This is bad code.
-
5:17
Now, in this case it's code that works, but you're not guaranteed.
-
5:21
Here's why.
-
5:22
In a general case, if this middle name value happens to be nil and
-
5:27
you forget to write this if else check for a nil value, then your code will crash.
-
5:35
To make this work,
-
5:36
you have to write the nil check first using an if-else statement.
-
5:40
And it's really easy to forget or
-
5:41
ignore it because it works in the few situations that you've imagined.
-
5:46
It's so much easier to write just one character than all of this.
-
5:50
Now, as we learn more, there are going to be places where using this exclamation
-
5:54
point is simply unavoidable.
-
5:56
But those places are very few.
-
5:59
Honestly, when I see students' code samples,
-
6:01
if it contains an exclamation point, I consider myself having failed at my job.
-
6:06
This is really something you need to avoid until you know more about
-
6:10
when it's useful.
-
6:12
Now that you know how absolutely not to do things, let's look at the right way.
-
6:17
Most of you will think the right way involves writing more code,
-
6:20
but that's okay.
-
6:22
The job of a programmer isn't to write fast code with as few words as possible,
-
6:26
it's to write safe code.
-
6:27
With that, let's move on.
You need to sign up for Treehouse in order to download course files.
Sign up