If you are a beginner Optionals can be a bit tricky to understand even though you may have found your way around it. In this article, we gonna look at the underlying principles behind SwiftOptionals. Before beginning, I assume that you know the basics of Swift. If not please follow my YouTube Videos and come back here.
One of the major advantage Swift brought in compared to other programming languages is the Optionals. Optionals allows you to store a nil value in a variable which is almost impossible in other languages. If a nil value found in a variable the program would crash instantly. But we can avoid it using Optionals in Swift. Let’s look at the step by step example below.
Let’s create two variables as below. As you can see myFirstName variable is an optional variable as it has a ? after the variable. You can print both myFirstName and myLastName without any issues even though myFirstName variable is nil. You could never do this in almost any other programming language as the application will crash.
let myLastName : String = "Mayu"
print(myFirstName) //prints nil
print(myLastName) //prints Mayu
So, now let’s assign some values to the myFirstName variable and see how the values are being printed. As you can see below, the myFirstName variable which is an optional shows in print that it’s an optional. We’ll learn about how to get rid of it towards end of the lesson.
let myLastName : String = "Mayu"
print(myFirstName) //prints Optional("Jay")
print(myLastName) //prints Mayu
We also should note that we can’t let optional variable to interact with other variables as it’ll not just work. It may work with String type of variables without crashing the application but it’ll crash the application if you try and do it with other variable types. Look at the example below which is based on int data type.
let numberOfCats : Int = 1
let numberOfParrots : Int? = 4
print(numberOfDogs) // prints 2
print(numberOfCats) // prints 1
print(numberOfParrots) //prints 4
print(numberOfCats+numberOfDogs) //prints 3
print(numberOfCats+numberOfDogs+numberOfParrots) //app crashes here
The app is crashing when you try adding numberOfParrots variable with other variables because numberOfParrots variable is an Optional type of a variable and it should be unwrapped before being used with other variables for various operations.
Forced Unwrapping
Let’s look at the easiest and dirtiest way of unwrapping an optional variable. We can do it simply using the ! by end of the optional variable but it’s not safe and if the optional variable is nil then the application will crash. In order to unwrap the numberOfParrots variable we’ll add ! by end of the variable and we’ll get something like this numberOfParrots! Let’s look at the example below.
let numberOfCats : Int = 1
let numberOfParrots : Int? = 4
print(numberOfCats+numberOfDogs) //prints 3
print(numberOfCats+numberOfDogs+numberOfParrots!) //prints 7
Let’s change the value of numberOfParrots to nil and check. Now the program will crash.
let numberOfCats : Int = 1
let numberOfParrots : Int? = nil
print(numberOfDogs) //prints 2
print(numberOfCats) //prints 1
print(numberOfParrots) //prints nil
print(numberOfCats+numberOfDogs) //prints 3
print(numberOfCats+numberOfDogs+numberOfParrots!) //app crashes
App crashes because only a Optional variable can hold a nil value. Since we’ve unwrapped the variable with the ! sign, it’s no longer a optional variable. So the app will crash as it has a nil value. So how do we over come this?
Safely Unwrapping
Safely unwrapping means, we check whether the optional variable has an actual value or it’s holding a nil value. If it’s holding an actual value then we’ll proceed with the intended operation else we’ll print an error method or take up alternative path. Let’s look at the example below where the numberOfParrots variable is safely unwrapped.
let numberOfCats : Int = 1
let numberOfParrots : Int? = nil
print(numberOfParrots)
print(numberOfCats)
print(numberOfParrots)
if let parrotsCount = numberOfParrots {
print(numberOfCats+numberOfDogs+numberOfParrots!)
}else{
print(numberOfCats+numberOfDogs)
}
As you can see above, the if condition is used to check whether numberOfParrots variable is nil or not. If it’s not nil then the if block is executed. If the variable is indeed nil then the else block is executed.
I hope this article brings you an understanding of the purpose of Optionals and how to take advantage of this cool nifty feature available in Swift. Good luck.
Subscribe to our mailing list
Subscribe to our mailing list and receive a free udpates
Leave a Reply