Optionals in Swift

Hey everyone! It’s Ashank again, and today we will be talking about optionals in Swift. The concept of optionals is a surprisingly easy one, but it is a concept with several applications that you will use multiple times throughout iOS app development. With that being said, let’s get started!

 

What Are Optionals?

Optionals are essentially types of variables that can have a “nil” or a “null” value rather than a specific value like variables that we have been using for the past few posts. Optionals can be assigned a “nil” value or a normal value based on the data type as well. Optionals are useful because sometimes you may need a variable set to nil, and the only way to do so would be to declare the variable as an optional. Furthermore, some methods return optional variables, so it is useful to know how optionals function.

Let’s take it to XCode to explain these concepts further. First, declare two variables: myOptional and notOptional, and declare them as type Double. Next, put a question mark besides “Double” in the myOptional declaration.

 

 

Screen Shot 2015-10-27 at 10.00.34 PM

 

 

As you can see, adding the question mark next to Double in the myOptional declaration instantly shows a console output of “nil.” Whenever you want to declare an optional, put a question mark next to the data type of the variable. To test the difference between the optional and the non optional, let’s add two print statements underneath the variable declaration. Print both variables out and see what you get as the output.

 

 

Screen Shot 2015-10-27 at 10.04.43 PM

 

 

myOptional prints out “nil” since it is an optional and is automatically assigned as nil. However, since we did not place a question mark next to Double in the nonOptional declaration, we are essentially telling Swift that this variable requires a variable. If we add a value to nonOptional, you will see that the error goes away.

 

 

Screen Shot 2015-10-27 at 10.07.47 PM

 

 

As you can see, the error next to the print nonOptional statement has disappeared. If we set myOptional equal to a double value, we will see that there will be no error when we print the variable.

 

 

Screen Shot 2015-10-27 at 10.10.44 PM

 

 

Giving the myOptional variable an actual value does not give us an error. This is because an optional can be either nil or an actual value. When we print the optional with an assigned value, the console outputs “Optional(1.01),” indicating that Swift recognizes that myOptional is in fact an optional.

Now, let’s switch gears to functions that return optionals. If you do not know what return statements are, please go back to my previous post where I have explained in detail the function of the return statement.

Let’s talk about the function toInt(). toInt() is a function that converts any variable to a integer. So if we have a String “1,” we know that it’s a number, or an integer, but Swift regards it as a String because that is what we declared the variable data type as. If we want Swift to convert it to an integer, we use the toInt() function.

But what if we call the toInt() function on a String that is not a number? Would this give us an error? The answer is no, because the toInt() function returns an optional. In other words, Swift can return nil or it can return the actual value. Let’s test this out.

Make two String variables, one set to “Hi” and one set to “1.”

 

 

Screen Shot 2015-10-27 at 10.23.37 PM

 

 

Now, let’s call the toInt() method on str and str2.

 

 

Screen Shot 2015-10-27 at 10.24.37 PM

 

 

As you can see, since toInt() returns an optional, str returns nil. This is because “Hi” cannot physically be converted to an integer. However, since “1” is an integer, the value returned is the actual, converted value, or 1.

Now, let’s say that we were sure that str2.toInt() was never, ever going to return nil in our program ever again. In that case, we wouldn’t need it set as an optional! To convert an optional to a normal variable that does not return nil, we can perform a task known as force unwrapping to convert the optional into a normal variable. Let’s try to see how this works.

First, you want to make another variable that is equal to str2.toInt(). Let’s call this variable convertedVar.

 

 

Screen Shot 2015-10-27 at 10.31.10 PM

 

 

Okay, so we are getting an error. Why? Well, we want convertedVar to be a normal variable and not an optional, so we left out the question mark next to “Int.” However, we are still receiving an error. Well, this error is because we are equating a normal variable to an optional. Remember that the toInt() method returns an optional! To force Swift to convert str2.toInt() to a normal variable, we need to add an exclamation mark after str2.toInt().

 

 

Screen Shot 2015-10-27 at 10.34.32 PM

 

 

Immediately after adding the exclamation mark, we see that the error disappears. This is because we force unwrapped the optional by adding the exclamation mark, essentially converting it into a normal variable.

The concept of optionals is not hard to grasp, but as I said before, there are several applications of optionals that you will be using in the future as an iOS app developer. You can check out my previous posts if you are having any difficulty with any other aspect of this post. I’ll catch you guys later!