The course is part of this learning path
This course will provide you with a comprehensive understanding of the fundamentals of Swift. We're going to learn about variables, constants, arrays, dictionaries, sets, if statements, and more!
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi, within this lecture, we're 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 these optionals and just save it on my desktop. Okay. So, optionals are 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 apt 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, that's 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 want to assign anything. Okay. And if I call my name that uppercase as you can see, it says that variable must be initialized. So, I haven't initialized it yet, but I want to call this. So, how do I do that? It's kind of different thing because in the most cases, we're going to get the string from the user or from the internet and we don't know if this 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 uppercased.
Okay. So, if we say string optional. So, this question mark actually stands for optional. Okay. This means that this name is going to be there probably what we're 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 uppercased 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. Gave our my string and this is an empty string. This is not new. 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 executing bad instruction. 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 and raptor. Okay, putting an exclamation point. You have to be very careful because if you choose the wrong thing, it will 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 going to show you all of this. Don't worry about it. So, right now I'm going to make this optional and I will run this again. And as you can see, my app will not crash but it will produce some milk. 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 that lower cased, for example. And if I do like this, it will give me an error because we cannot force unwrap a value for 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 going to 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 interior. It's a 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're going to see why right now. I'm just trying to convert the 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 interrupt 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 the double question mark? And how about just inserting the exclamation point here because we can actually convert this into an 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 into 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 apples drink rather than an integer. So, it just crashed. Okay.
So, it crashed our code as you can see we got this bad instruction add 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 the 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 it 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 will just take the zero over here, the default value and multiplied 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're trying to create a variable and if it succeeds, we're 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 this, if let my number into 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 a 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 interior 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's 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 5 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 going to stop here and following the next section, we're going to build an example. Very simple calculator in order to practice all this information that we have learned.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.