‫-: Hi, within this lecture we are going to go
‫into a new subject called Optionals.
‫In order to do that, I'm going to close this
‫down and I'm going to create a new playground.
‫So I'm going to name this Optionals
‫and just save it on my desktop.
‫Okay? So Optionals are a great tool to work
‫and make our code safer.
‫So by safer, I mean immune to crashes.
‫So last thing that we want when someone
‫uses our app is app to crash. Okay?
‫So Optionals are actually here to make that preventable.
‫So let me show you what I mean.
‫I'm going to create a new string called my name
‫and this will be James.
‫Okay? This is perfectly clear.
‫So if I do my name dot uppercase, for example
‫I will see James being uppercase on the right hand side.
‫So we, we have seen this, right?
‫We know what this does.
‫So what happens if I don't want to give
‫the James value in the first place?
‫So as you might remember,
‫defining and assigning are two different things
‫so I don't wanna assign anything, okay?
‫And if I call my name dot uppercase
‫as you can see it says that variable must be initialized.
‫So I haven't initialized it yet, but I want to code this.
‫So how do I do that?
‫It's kind of different thing because
‫in the most cases we are going to get the string
‫from the user or from the internet and we don't know
‫if the string is going to be, actually there or not.
‫So maybe user won't give any string, give any name
‫and they will click the uppercase button anyway.
‫So they won't even write their names,
‫but they will click the button
‫that will make their names uppercase.
‫Okay? So if we say string optional,
‫so this question mark actually stands for optional, okay?
‫This means that yeah
‫this name is going to be there probably
‫but we are not even sure.
‫So if I do that, this will give me two options.
‫So if I come over to my name
‫since my name is an optional string right now rather
‫than a regular string, I have to choose between two things.
‫If I want to make this optional, again
‫I can just say my name, optional dot uppercase,
‫and if if I run this, I won't get any error, okay?
‫But I will get the value of nil.
‫As you can see, nil means nada, nothing, okay, emptiness.
‫So it's not an empty string, it's nil,
‫it's not even initialized yet.
‫So let me create another string over here.
‫Okay? VAR my string, and this is an empty string
‫this is not nil, but my name is nil.
‫So nil versus empty string.
‫Two different things because you remember the difference
‫between defining and initializing.
‫This my string is initialized,
‫my name is not even initialized.
‫So if I don't want to initialize something
‫and if I don't know whether it's coming or not,
‫I use Optionals.
‫And this way this will produce nil
‫but it won't crash the app for me.
‫And the other option is to come here
‫and put an exclamation point.
‫Okay? So you remember the other option.
‫If you do it like this, it will show you two options.
‫And the other option is to say exclamation point.
‫And this means that I am perfectly sure
‫that there is going to be a name initialized, okay?
‫The user will give the name, but if they don't
‫and if they click on that button anyway
‫as you can see we got the error,
‫executed bad instructions.
‫So this means that your app has just crashed
‫and this is the last thing
‫that we want to happen in our app.
‫So there will be a lot of times
‫when you will have to choose whether to make
‫this optional or force unwrap this.
‫Okay? Putting an exclamation point, you have to
‫be very careful because if you choose the wrong thing,
‫it'll break your code.
‫So you cannot go with Optionals every time,
‫by the way if that's what you were thinking.
‫And you cannot go with force unwrapping every time as well
‫because this is very much obvious
‫you can actually crash your app.
‫So we have some techniques to make this right.
‫I'm gonna show you all of this, don't worry about it.
‫So right now I'm gonna make this optional
‫and I will run this again.
‫And as you can see, my app will not crash
‫but it will produce some nil.
‫So let me go to other examples.
‫So I'm going to create a new string
‫and I'm going to call this Lars.
‫If I go to my string, okay, if I say my string
‫exclamation point dot lower-cased for example
‫and if I do like this, it will give me an error
‫because we cannot force unwrap a value of non-optional.
‫So this is non-optional type string.
‫This is a regular string.
‫We cannot do that over here, okay?
‫We can only put exclamation point
‫or put an optional when it's an optional type.
‫So in this my name case, this is optional string, okay?
‫So we have to be careful
‫with this question mark and exclamation points.
‫So I'm gonna give you a very concrete example
‫where you will understand why you cannot use
‫Optionals every time and why you cannot use
‫force unwrapping every time as well.
‫And what is the optimal choice
‫when it comes to a decision like this?
‫For example, let me create a string over here, okay
‫this is my age, but it's not an integer.
‫It says string
‫as you can see it's wrapped around a quotation mark.
‫And before we didn't try to convert a string
‫into an integer because we have to know
‫about options in order to do that.
‫And you are gonna see why right now I'm just
‫trying to convert this string into an integer.
‫Can I do that with my age?
‫Of course I can, right?
‫So I can just say my integer, okay?
‫And this will be INT wrapped in my age.
‫So this will convert into my age into an integer.
‫But let me just multiply it by five for an example
‫but it will give me an error.
‫So it will just say this is an optional
‫and because we don't know if we can do this operation
‫we don't know if we can convert
‫this string into an integer.
‫So we have to use a double question mark
‫or an exclamation point in this stage.
‫So how do we do that?
‫What's a double question mark?
‫And how about just inserting a exclamation point here
‫because we can actually convert this
‫into a integer, right?
‫So let me try that.
‫Let me try this with an exclamation point.
‫And as you can see, I managed to convert this
‫to an integer and then multiply it by five.
‫But what happens if I change it to an apple?
‫So in this case our user decided to
‫give an apple string rather than an integer.
‫So it just crashed, okay?
‫So it crashed our code.
‫As you can see, we got this bad instruction error over here
‫so it doesn't work.
‫If we are sure that user is going to give us a number anyway
‫then maybe it makes sense to put this exclamation mark.
‫But if that's not the case, then we shouldn't do it.
‫For example, let's do the double question marking.
‫So double question mark is to provide a default
‫when optional value is actually nil.
‫So in this case this will attempt
‫to convert this into an integer.
‫And if doesn't, if it cannot do that, if it fails
‫then we will provide a default value, okay?
‫And for default value, we can just say something like zero.
‫And if we do that, if we run this, we will still
‫get 25 because the attempt is going to succeed.
‫But if we do it something like this
‫if we do an F over here
‫it won't convert this into an integer
‫but rather it'll just take the zero over here
‫the default value and multiply it by five
‫and the result will be zero.
‫But there will be times that I don't want that.
‫What if I don't want to get a zero as an output?
‫What if I just try to display a message text
‫or something saying that you have to give me
‫a number to user because that's not good.
‫Zero is not what I'm looking for here.
‫I'm looking for a number.
‫Is there a way to do that?
‫Of course there is a way we use something like this.
‫If let, it means that we are trying to create a variable
‫and if it succeeds, we are going to do the following.
‫If it doesn't, we can do whatever we want.
‫Okay? And this won't crash the app.
‫This is the main difference.
‫So we do it like this, let me comment this out, okay?
‫Or we can just leave it as it is, doesn't mean anything.
‫So I can just do it like as if, let my number,
‫INT my age and open parenthesis.
‫So it means that we will attempt to convert this
‫into an integer, and if this succeeds
‫then we can go to the coding block.
‫So whatever we write over here will be happening
‫if this succeeds, if it doesn't, it won't crash the app.
‫So we can create an new variable, for example
‫and multiply it by five.
‫So actually we can use my number as well
‫because we have already created this
‫and we can just print this out or we can assign it to be
‫we can assign this to be a new integer
‫and we can print that out as well.
‫So if let, my number is actually integer
‫my number, my age, print my number multiplied by five.
‫So this gets zero in this case.
‫And I can add an ask statement here as well, right?
‫So I can just say wrong input or give me a number.
‫So if I run this, then I will see wrong input.
‫So that's perfect, that what I was looking for.
‫In this case, my user will also know,
‫that he or she is doing something wrong.
‫I have to give a number.
‫I don't make this work with strings or texts.
‫I have to give something like five or 10 or 20.
‫So if we come across in a situation like this,
‫it means that we have to use this if let control,
‫if let statement in order to make our code safe
‫and also in order to keep things going.
‫So we have to display some message
‫to the user or we have to do something else.
‫If this fails, then this is the perfect choice for us.
‫So we're gonna stop here and following the next section
‫we're going to build an exemplary,
‫very simple calculator in order to practice all
‫this information that we have learned.