Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.2 Optionals

The Swift language has some very interesting features built into it, and one of them is the concept of Optionals. Optionals are a new type-safe feature of Swift. They’re similar to nil pointers in Objective-C, except that they work for any type, not just classes.

Related Links

2.2 Optionals

The next interesting topic that I feel definitely bears some explanation, at least a little bit, is what's called an optional within the Swift programming language. Now in other languages, you might be used to referring to this maybe as a nullable type, or something like that. But basically, what this means is that at some point during the execution of your code, you're going to do some sort of operation. You're gonna use a method or something that's going to return a response, or return a value, that may or may not be nil, or be null, depending on what sort of languages you're used to coming from. So let's see an example. So let's say we're to create a variable, and we'll call this stuff, and I'm going to initialize it to the string a. Now this is all fine and dandy, and now there's a nice little helper method on the string type, and we're gonna talk about this a little bit later when we start to get into the Swift standard library. But for now, just know that there is a method called toInt, and as you can see here, it returns an integer with a question mark. Now, what this represents is the fact that this is an optional value, which means there may not be a value. This may not return a valid int, and if that's the case then it will return nil. But if it does have a valid integer value, then it will have that, the value of that integer. So let's go ahead and take a look at this. So if I wanted to create another variable, and we'll call this intValue, and we'll say that this is going to be equal to stuff.toInt. Now after I execute this line, you're gonna be able to see because we're using the playground that the value of intValue is actually nil. And that's to be expected, because a, the string a, does not represent an integer. So if I were to change this to 1, the values are gonna change here, and you're gonna see that this is turned into Some 1. Now, Some is the visual cue within these playgrounds that this is actually an optional. And if I were to click on the little eye here, or the Quick Look, you're gonna see that definitely, this is a Some 1, and if you click on it again, the actual value here is going to be 1. So I can come down and look at int value here, and I can start to do some operations on this particular integer that are built in to the int type. And in this case, let's say I wanted to advance it by a certain amount, I wanted to advance it by 2. And so, as you can see here, a couple of things have happened. By using the advancedBy 2, I've actually increased the value that was in intValue by 2, so that's 1 plus 2, it's returning 3. But as you can see here, it's returning Some 3. And why is that? Well, what we're doing here is, we're actually doing something, or an operation, called optional chaining. And what's happened here is, I did not put in this question mark here, that was put in for me. So what this is doing is saying, and this has to be one of my favorite features as far as the Swift programming language. I typically come from a .NET world, and in C#. Now in, in that language, I have to explicitly ask if a particular nullable value has a value or not. So that definitely lengthens the programs that I have to write, because if I get back a nullable value, or a bunch of nested nullable values, I have to say, okay, at this level, does it have a value? Yes? Okay, then in that if block, I can come down and say, okay, does the next value have a value? And I have to keep going down and down, and there's a lot of nesting involved there. But in the Swift programming language, I don't have to worry about that. So at this point, it knows that the int value is going to be an optional just because of the fact that what's returned from toInt here is actually going to be an optional. Now, I've done another operation on that int value, so I've done an operation on an optional, which is going to start this pro, process of optional chaining. Now, advancedBy, if I come into view and take a look at my utilities and show the quick help inspector, we're gonna see that advancedBy returns an integer. So at this point, I could come in here again and say advancedBy, and I could do advancedBy 2 again this time, and now we're getting 5. But as you can see, we only got the one question mark here. And question marks, like I said, within Swift denote the presence of an optional. So what this is actually allowing us to do is say, okay, I know that int value isn't optional. But if it has a value, then when I call advancedBy, I want this to function properly. I want this to actually return a value. In this case, it was 3. Then, I'm adding another method here. I want to advance that res, that returned value by another 2, and get 5. So this is all working fine. But watch what happens if I change this to a. Now, in other languages you may expect to get an error here, because I'm assuming by the code that I'm writing here in Swift that int value contains a valid integer. But I know in this case it doesn't. But instead of me having to say, okay, maybe calling a property or a method here to check to see if this is valid. And if it is valid, then advancedBy. And then, if that is valid, then do another advancedBy. What the Swift programming la, language allows me to do is say, okay, I know that this is an optional. So I'm going to put a question mark here, which is going to say, If this is a valid integer, then I wanna call advancedBy. And if that's a valid integer, then I wanna call advancedBy. But if at any point this is not a valid integer, I'm not going to get errors, I'm not gonna get runtime errors and have to go through and debug all of that code. I know that the Swift programming language has got my back to say, this is not a valid integer, so you're just gonna get nil back. Now that can definitely cause some other issues down the line, so you're gonna have to do some checking for nils and, and do some logic based on that. But at least you're not gonna get some really nasty errors that you're gonna have to dig back through, because Swift has got it taken care of. So this is one of, probably one of my favorite features of the language, this whole concept of optional chaining. So optionals are gonna become very important when we start to talk about custom classes and types that we're gonna start getting into in the next couple of lessons. But definitely keep in the back of your mind this concept of optionals. And whenever you are working in a playground, those are gonna be denoted by values that contain the word here, Some. So that is the concept of optionals.

Back to the top