Hey buddy what's going on.
This is Caleb with Debb slopes dot com and in this video we're going to talk about optionals and optionals
are an amazing part of the swift programming language.
And I want you to think about an optional sort of like a rapper or like a box.
OK.
Now it can hold something right like a box can have something inside of it.
A wrapper can have some type of maybe food or even like a drink if you think of like a drink container
it can hold something or it can be empty.
OK.
That's like that's what an optional is sort of like it can have a value or it cannot have a value.
Now imagine you got a package delivered from Amazon.
When you pick it up off your front porch there can be something inside but it's possible that at the
fulfillment center there was a mishap and maybe your box is empty which of course that would be really
disappointing and kind of a lame thing but that is possible.
The idea behind optionals is that we can safely use the box without having to worry what's inside.
OK.
That is the cool thing about optionals So let's go ahead let's pull open a brand new X code playground.
So open up X code and in this little welcome to X code menu click get started with a playground and
we're going to do a blank playground and I'm going to call this shoes optional.
Just you know instead of clothing optional just say shoes optional little more appropriate anyway create
it and let's pull it open.
Full size.
There we go.
By the way if you're wondering how I'm doing that I can make it take up the left side of the screen
the right side of the screen.
Anything like that.
Pretty neat.
It's a little app called shoot.
What is it called a magnet.
Right.
It's called Magnet.
There's another one called flexi glass.
I just happen to like magnet a little bit better.
Anyway let's get started.
So I'm going to go ahead and get rid of that variable there and I'm going to get rid of that little
commented line there.
And I want to show you how optionals can work and I'm going to use some images to represent them.
So I'm going to create an optional image and I'm going to create a non optional image so I'm going to
show you how you declare a variable as optional.
So go ahead and type let optional image and we're going to make it of type UI image but I'm going to
declare it is optional by putting a question mark at the end of the type declaration.
Now of course as it currently exists it is optional.
It can have a value.
It can not have a value meaning it's able to be nil or it's able to be or to have a value.
And I'm going to go ahead and give it a value just for the explanation purposes of this.
And so I'm going to insert an image literal from the editor menu and I'm going to drop in that image
right there.
OK now I'm going to add a viewer so we can see the image.
All righty and that's great.
So there it is.
This is an image representation of what an optional is like it's a box but it can be empty meaning it
can have a nil value.
Now let's go ahead and create let non optional image and we're going to make that of Type II image K
and we're not going to put a question mark meaning it has a literal value.
We're going to go ahead and insert an image literal and I'm going to use this image to represent that.
And when I pull open this viewer you can see what I mean.
This non-optional image.
Well it's an image but it represents an optional with a value of five.
So this optional can have a value of 5 or can have a value of nil.
That's pretty cool.
Now I'm going to show you ways that you can actually use optionals and then some things we need to be
careful about as well as at the end I'm going to show you kind of a real life example of using optionals.
So we're going to go ahead and talk about forced unwrapping.
So go ahead and type a variable here.
Var optional number k.
And we're going to make it of type int.
Although we're going to make it optional.
OK so this could be for something maybe that we don't know if it's going to have a value or not.
Although I'm going to set it to have a value of 5 K so it's optional but it has a value of 5 and I'll
show you how this works in just a moment.
Next we're going to go ahead and just create another variable of type and and make it equal to 5 as
well.
Now you'll notice the second one is not optional.
So what I can do here is I can say if optional number is not equal to nil OK meaning if it is not nil.
We're going to print and we're going to put something in the print statement that says optional number
has a value of and will use string interpolation here to pull out the value of optional number and we
will go like that and pass it.
The value for optional number because we have given it a value right.
We say optional number has a value of optional number.
Now you'll notice when it prints out it says optional number has a value of optional 5.
Now you're wondering well why is that.
This is a way that swift keeps a safe when we're programming.
Since it is optional it's passing in the value for 5 but it's keeping track that it's an optional value
and that it could be nil.
Now watch what happens if I change it to nil meaning I give it a nil value of value of nothing.
You'll notice nothing happens because we're checking to see if optional number is nil.
So let's turn it back to 5.
But I said we're talking about force unwrapping.
Now this optional number variable has an optional value of 5.
But in order to actually access the real value we need to use an exclamation mark to force unwrap it.
It's like we're opening the box and taking the value 5 out.
We're actually getting it from the box.
So that's Force unwrapping.
But watch what happens if I change this.
Let's just say I take away this conditional check.
This is to show you the danger of using an optional.
Watch what happens if I change it to nil fatal error unexpectedly found nil while unwrapping an optional
value.
That's a problem so this is where we need to be careful with force on wrapping.
You need to basically set up conditional code to check to make sure that it's not nil.
And you might be wondering well why would I ever have an optional value.
Think about if you're pulling data from a server and you're not sure that a certain value will come
back of a certain type every single time.
It would be better to set it as nil or sorry I said as an optional in case the value returns nil so
if that were to happen you could say if it's not nil.
Do something else.
And then here you could basically set it up so that it would handle any errors.
Ok that is just one way you could use optionals.
Kate gives us the option right.
That's the optional thing.
It gives us the option of basically using a value or a non-value.
So here we go.
Let's go ahead and move on.
That's for Sun wrapping.
That's how you take an optional value and you force unwrap it into something that is an actual value.
Pretty cool.
Now I'm going to go ahead and just close these viewers here they're giving us some trouble.
And let's move on.
So next up we're going to go ahead and check out something called optional binding.
OK.
Now what we're going to do with optional binding is we're going to basically take an optional value
and we're going to bind it to a non optional constant.
But the cool thing is that we will use an IF statement to handle what happens if the optional number
comes in as nil.
So check it out.
If we go ahead and type if let And then we can say constant number K-Stew we just created a constant
called constant number.
We're going to set it to be equal to optional number.
All right so now we have a constant that has a definite value of optional number.
But what happens if constant number is not.
What if we can't set the value of constant number what if optional number is actually nil.
That would cause a problem.
So what we're going to do is we're going to print let's print out what we see here.
Constant number has a value of.
And we'll print out constant number.
OK.
Now when this runs you'll see that it actually does print.
We are creating a constant called constant number and when it runs lets run it and you'll see it'll
print out constant number has a value of constant number but it's not doing anything.
That's because our value is nil.
And that's where our else block comes in when we're doing optional binding we're basically binding an
optional value to a constant.
But we're using an if else so that we can handle both cases value or no value.
So if we can create the constant and set it to be optional number it'll print that out.
Otherwise we will print what is it.
Let's see optional number is nil because that's what it means it must be nil if that's if that's what
is happening and as you can see optional number is nil.
And so that is where we could handle errors just like I said above.
But the cool thing is that we can use constant number as a way of creating a non optional value do you
notice how we don't have to force on wrap this.
Watch what happens when I set it to be a value of five optional number and Konst that number has a value
of five that basically allows us to create a non-optional constant from an optional.
Pretty cool.
So that is optional binding you know what let me go ahead and put some notes here so we don't forget
this was force unwrapping force unwrapping.
Whoops.
Anyway moving on.
So let's go ahead and I want to show you another example.
I'm going to create a function here called into printer and it's basically just going to print out an
integer value.
Now I can also use optional binding within the guard let check it out.
Guard let constant number just like above.
And we're going to set it to be equal to optional number.
If we can't do that right.
Meaning if it's nil we're just going to go ahead and say return just like that.
Now assuming that we do get a value we're going to go ahead and print constant number has a value value
of
what is it optional number a constant number.
We'll just say constant number because it will be the same as optional number.
Cool cool.
So we've used the guard let to say if optional number has a value.
Set it to be equal to or set constant number to be equal to optional number.
Otherwise return and get out of the function.
So let's go ahead and let's call in printer.
Whoops there we go.
And when it runs you'll see that it prints it out right there a third time.
But watch what happens if I set optional number to be equal to nil or have no value.
It doesn't print.
The only one that prints is our first one where we forced unwrap the value or sorry where it where we
tried to bind it.
But if we couldn't bind it then we would print out optional numbers nil.
So change it back to have a value 5 and we're going to move on now.
We're going to talk about what are called implicitly implicitly unwrapped optionals.
OK.
So implicitly unwrapped optional is basically where we are going to force unwrap a value from the very
beginning.
So to do that we can just say I'm going to assume that a value is coming in as maybe 5 so let assumed
value of type int equals five.
But if we're going to make this implicitly unwrapped we're going to go ahead and put an exclamation
mark.
That means we are force on wrapping that value.
So what we're going to do next is we can just say let implicit value of type and equals assumed value.
OK.
So we are able to create a value you can see over here that it does create a value of 5 but watch what
happens if I make it.
Nil we get an error unexpectedly found nil while unwrapping an optional value.
So unless you are one million percent sure do not implicitly unwrap optionals this is kind of a last
ditch thing.
It's kind of if you have to if you're not 100 percent sure that there's a value.
Do not force on rap or implicitly unwrap values like that.
OK it's not good practice.
OK so next up there's kind of a really cool thing I want to show you with optionals called nyl coalescing
and something called using the ternary operator.
So let's go ahead and call and nyl coalescing and using turn very operator.
So we're going to create an optional integer.
OK so go ahead and type let optional end of type int equals nil and you'll see if we do this it's going
to not let us it'll say we can't use nil on something of type int because nil is not an integer value.
But if we set it to be an optional that means that we basically put that in a box and we say OK the
box can have a nil value or it can have a numerical value so that's cool and all but what if we want
to create a variable but we're not sure if optional end is going to have a value or not.
Maybe we're pulling data from a server so we can say let results equals and watch what we can do.
We can say it equals the optional and but what if you're not sure.
Now if it comes back with value like if we gave it the value five that's great and now optional is equal
to five therefore result is equal to five.
But if it comes back nil we might not want to set it to be nil right.
That would cause a crash in our application.
So what we can do is we can use the turn every operator by doing two question marks.
Then after that we can type is zero.
And what this is basically saying and you'll see it as it prints out is if we don't get a value set
it to zero.
Otherwise set it to be optional.
And so this is kind of like like else.
Think.
Think of it like this optional it has a value.
Let results equal optional.
And if there's a value otherwise set it to be zero.
Meaning if Neal comes through it's going to set it to zero.
Just like you can see now if I said it to five you'll see that the optional will print out as 5 because
it adds value.
Really really cool.
So let's go and set it to nil just because it's pretty cool that it's using the ternary operator there
and last but not least we're going to go ahead and use a real world example with Comic-Con.
I'm going to Comic-Con next month and I'm really excited.
So I was thinking about it and thought it was a perfect example for what we're going to do.
So first of all we need to create a class for Comic-Con attendee and we need to create a class for their
admission badge.
So go ahead and type class comic con and the K and we need a variable we need an admission badge like
so of type admission badge which That's a class will create in a second but we're going to make it optional
because everybody who goes to Comic-Con they might not have a badge from the very beginning they might
want to buy tickets when they get there.
So we need to create a quick initialiser and we're just going to say badge of type admission badge and
that of course is optional because they may or may not have a badge and we'll just say self admission
badge is equal to badge just like that.
OK so now we're going to create the admission badge class so that our attendees can actually attend.
That would be helpful.
So type class admission badge and let's go ahead and just say for Comic-Con we're just going to give
a variable number of days.
That's usually how Comic-Con tickets go.
And every bag is going to have an explicit value so we don't need to make it optional and we just need
to initialize it.
So go ahead and type number of days of type int and we'll set self number of days to be equal to number
of days.
Very cool.
So what we're going to do is we're going to create an admission badge.
We're going to create a Comic-Con attendee and we're going to give them that admission badge so go ahead
and type let admission badge equals admission badge and we're going to pass in a number of days.
Let's just give them a three day pass and now we're going to create a Comic-Con attendee.
So let attendee equals Comic-Con attendee and need to give them a badge so go ahead and pass in the
admission badge.
And just like that we have an admission badge and we have a Comic-Con attendee which is really cool.
But if I was building an app where I wanted to show how many days left you have on your Comic-Con badge
I might type something like this if let days a tenable meaning I have X amount of days left.
We can check that by going in to attend the admission badge end did you notice admission badge is optional.
You can see that right there and watch what happens when I type that number of days.
That's because I might not have an admission badge.
OK.
So it's putting an optional there so that it is safely unwrapping and safely using the value in my admission
badge.
So if there's amendable is equal to number of days which in this case is three we're going to go ahead
and print whips.
We're going to print this attendee can enter Comic-Con for.
We're going to print the number of days here days a tenable days.
Else we're going to print something else.
Now we didn't think what would we print if days a tenable is not equal or if days a tenable number of
days comes back as.
We need to say this person has not yet purchased a ticket.
Please refer them to the ticketing window and watch what happens when we print this out.
OK.
So it says this attendee can enter Comic-Con for three days but watch what happens if I remove the admission
badge remember for a Comic-Con attendee admission badge is optional so I can pass in Knill.
What if I did not have an admission badge.
If it comes in is nil.
Watch what happens.
This person has not yet purchased a ticket.
Please refer them to the ticketing window because I was using optional binding and that basically created
a constant here called days attend table and I set it to be equal to the number of days from the admission
badge.
But if the admission badge is nil then I get an optional value of nil and it prints out here.
This person has not yet purchased the ticket.
This is called Let me put it up here at the very top.
This is called optional chaining and it allows us to change values of optionals together so that we
can get a value or handle errors when it returns nil optionals are incredibly helpful and I really hope
that you can see that these are just some super helpful small examples of using optionals And of course
if you're confused or if you want to know more.
I would highly recommend digging into the Apple documentation.
There are tons of examples there.
So amazing work with this video.
And let's head over to the next one.