Hello and welcome back in this lesson you're going to talk about swift optionals swift optionals might
seem a little bit confusing at the beginning.
So I'm going to try to simplify it as much as I can in this lesson.
So if the optionals has only one meaning to make sure that your valuables have a value when you use
them.
So essentially it's a way for us to make sure that you don't act as a value that doesn't have anything
in it.
You don't access any value which is going to cause your application to crash.
So let's go ahead into export and see how we could actually use them.
I'm going to go to school and I'm going to start a new Actually this time I'm going to start a new command
line tool.
I'm going to say command line tool strafed and then when I call it my option is to
and my optional tours I'm going to produce three different variables.
I'm not talking about all three of them.
One of them I'm going to call it the optional string.
And that's going to be a strong question mark.
The other one I'm going to call it the VAR non optional string.
It is of the type of string that you keep the same formatting and it says hello.
And then the last one is going to be there.
On the wrap string.
And that's the type of string.
So I want to spend a few seconds having a look at this.
And then when I explain what they mean.
So now let's try to understand what do these three things mean an optional a string with a string questionmark
means this value could be nil.
It's possible that it doesn't have anything inside it.
So whenever you want to use it and we know the value exists we have to actually access it by saying
something for instance like optional a string and we have to force it.
We have to say now we know the value exists.
Obviously this is not going to print nilla for us because it is not there but for us it means that it's
likely you could be in in a few minutes.
I will show you how we go about doing this in a safe way because this is really not a very safe way
of forcing it because for instance if you set this value was my user name and it is nil because user
may not have entered the username in the text field.
And then if you had a function hypothetical function that says like innovate the user name and you entered
this in here.
Then obviously this is going to crash because it is going to say well you're trying to log in with something
that doesn't exist all you're trying to do something like luggin with Ned.
So I'll show you in a minute how we go about being safe with it.
However whenever you have an optional string it means this could be nil.
The other one is a non optional listing.
The Raiders are getting more used to using it may not actually string a string and that has a value
such as Hello.
So I could actually take this out and go ahead in here and say Prince.
Prince none optional string.
So what do you think is going to happen when I run this application now before I can even run it.
It tells me hey this variable needs to be initialized because it is not an option.
It must have a value.
So you either have to do it at a declaration time or you have to do it.
Now this string is headed.
So we'll use either of these two situations.
We use optional values for when we don't know if anything is going to show up.
For instance if I had a list of users something that we have done in the past and each of the users
has certain number of photos but they don't know if they necessarily have photos or not.
So I would have a photos or array of let's say you or I or not you images are strings.
And then this would be actually an array and that is optional.
I don't know if that thing actually is going to be mail or it is going to have IDs.
So that's one thing.
Well would they use non optional.
Not optional.
I will use it in situations where I know for a fact that that value exists and I know right off the
beginning.
So a situation like this where I'd have immediately said hey this value is going to be in this situation
our i'm I've just started the OP that the moment I started I'm going to read the value from my user
card username and then my username immediately is going to be our name of my youth.
So in that situation we use none option.
Finally we have something in of this caught on or off the street.
Will talk about it in more detail in a minute but just look in the stand how it works is when you have
optional listings.
If I go in here and say preens option on this string is going to give me an error is going to say this
is an optional string.
So how do you want to convert it.
Are you sure it exists like this.
Or are you that find that either way a safe way to do it when you have an up to this train.
And it is of that type whatever you want.
Exclamation might.
Then you can just place it in here because it will be treated right.
This charade as if it was non optional.
So once again this is not optional.
Treated as if you know the value is always there.
Up up this string is going to be treated as if it was not optional.
It means its value is initialized immediately somehow the value for it is going to show up real soon.
So with that in mind.
And let me read you all these things a little bit.
It's pretty nice.
That's that.
That's obviously not optional.
This is the forced on dropped what happened.
OK now let's see how are we going to go ahead about converting optionals into something that you know
makes sense.
So the first way is obviously force on writing for us on dropping which means we do something like print
in the print I'm going to say optional this thing and I'm going to unwrap it.
So whenever you have the sign of exclamation mark it's means on dropping either here are implicit on
dropped or in here forced on rap of course.
Maybe I can cut these and put them at the bottom because that's not what you're talking about.
We're talking about this one the optional one.
That's the force on rapping which is not safe not safe because you can't be sure if this value has something
in it.
As you can see in this situation right now I have the option of saying it is this thing but it is not
going to be safe to force it.
It doesn't have any value in it.
It's not going to crash.
It's going to give me a needle in this application.
But in general if you have a value that value might very well you know be like the example I gave you
were making it a logging in a user name.
So we have to make sure that you know we don't force it.
The other option is much better and much safer option is a conditional on wrapping.
So how do we do conditional mapping.
We have something called optional string.
This is condition not conditional
on mapping.
So we have a value called optional history.
I'm going to say if let's raise a string is optional a string then do something.
So I'm essentially saying let's end oystering be that one.
If it does exist do this.
Print our value exists for instance.
And then of course you can have an else which you decide about the other part of the other side if it
doesn't exist.
So if I run this application now obviously is going to crash in here because it's going to say you're
forcing your value that is near.
So I'm going to comment that out and then run the application.
And then it says value doesn't exist simply because let's raise the string has never done anything in
the optional string has always been known.
Now let's do one other thing.
What if you don't want to change this name.
So you could actually do this you could say if left optional a string is optional a string then say
value exist or let's just print the value.
I'm going to say optional is saying the same thing in the else.
Actually in the end I can't because you know I have to say it doesn't exist so again value doesn't exist.
But what if somebody in the middle before we did this a statement I would have said optional testing
is typing objective c.
Now the value is going to be printed.
So upshot is things you introduce them by a question mark at the end of them are optional values in
general optional are integer optional flawed optional array whatever it is.
If you want to force on them you just put an exclamation mark at the end of them.
It is not safe.
It is for when you're absolutely sure the value exists.
Otherwise we use a conditional on wrapping to make sure that if flat the same name if you want if it
equals to that and it doesn't become a value and there isn't that this thing will get a value is because
actually string in here it's not optional.
So I know I use the same name and it is becoming a little bit confusing.
So I'm going to go with there is a string.
I remember minor as a string doesn't have a question mark in front of it is not registering is in question.
No it is a non optional.
It's very similar to what we did here when we said that non optional is something I've said you feel
that's that thing.
It means that the value actually exists and then obviously based on that we can print it.
And if it's not that thing if the else is happening we could decide what about for instance your luggin
shouldn't happen because using name has been has not been provided.
So there's finally another way of dropping an option.
And that is using a type casting.
So what I'm going to get myself a little bit of space in here and I'm going to say if I have an optional
string I could do this I could say if option all this string as a string is not nil.
And don't worry about the format of an IF statement.
We will see that real soon in action in our next lesson which is about the control flow.
So if that is not the case if it is not in there it's means we have a value.
There is of course another easier way of doing this.
Optional on dropping which is which is not as.
As you know as funky as using an if let situation and that is this you could go in here and say if for
optional string is not there then do whatever you want to do.
For instance do this thing print that.
I know this is not going to crash our application simply because it only happens if optional a string
has a value in it.
In this case it doesn't.
So we know nothing is going to happen.
So really quickly once again just to recover things that we discussed in this lesson.
Actually I wouldn't mind if I rewrite everything.
Real quick with an integer this time.
So now I really have some some sort of understanding this should become a lot easier.
So I'm going to say our my in and this might mean I'm going to give it a value right at the very beginning.
And if you want I could actually say it is integer.
So this is not optional and it means she is already so perfect.
I'm going to have another one.
Let's call it next.
Integer is of that type integer and it's optional optional integer that needs to be on right before
use.
So we have to wrap this before we want to use it.
And then finally there are on the integer which is integer and it has a little exclamation mark it is
treated Azoff as if it was not optional.
So let's see what do we do with all of these.
If we go on to say print for instance to this thing are preens next integer next integer is an option
or a value is going to crash this application.
Now he's going to say you haven't really given me any value for it.
I don't know if the value is nil.
So you have to essentially give me a value if you try to force it say Hey I know it exists it's going
to say well no it doesn't exist and it's going to crash application.
So here's what you can do.
You can say well if the next event is not net then do whatever you have to be doing.
So that partially solves our problem a much better or more elegant solution for it is doing this saying
if we let our next gain being next in us if that was let then do whatever you want to do then do what
ever you want to do.
All right.
Otherwise it's still the net.
So that's about that.
I'm actually going to remove that because I want to write is for use we can have as a reference for
future.
So this will crash this one in here.
Obviously it says what it is doing and you can do things such as this print on the top end and you don't
need that exclamation mark.
He's going to treat it as everything here reads
as if it was not optional.
All right you might actually keep this up as as like a cheat sheet for yourself.
Non-optional us optional if it's Question My option is that of force on rap and it could potentially
crash you have to use it and you are really really short and conditional on dropping some type of conditional
on writing for when you are sure this thing actually.
For it makes sure for you that this thing exists thing you say let something to be that thing.
And finally when you want to use your implicitly arm dropped valueless now a more pressing question
for you might be so where do I use either of these.
Obviously use not option us for and you know what your value is.
Almost immediately you use your optionals for when you don't know what is going to be there or is it
going to be there.
And you use your implicitly on dropped values for situations where there are value for that value but
is going to be there or is going to be populated almost immediately.
So you're reading a user your user most definitely has in name and has a unique ID and then you own
your unique ID.
You state as to be a and implicitly on up to one because it is not there now but it will be immediately.
So that's about that.
With all of that in mind Arved Let's move on to our next lesson.
I have included a link to the optionals from the swift documentation.
I would recommend you read through that as well because that really helps as well.