Optional Chaining in Swift and Why it is Important
When building out your application in Swift, a lot of developers may encounter runtime errors when building their application. A very common cause for runtime errors is that the value that is being requested is nil. This means that the application is requesting a value that does not exist. The compiler doesn’t know how to handle this case and therefore creates a runtime error, crashing your application.
There is an easy way to avoid this runtime error by utilizing optional chaining. Today we are going to take a look into how to use optional values in your Swift code so that you can avoid those pesky nil runtime errors.
When reviewing a project or even going through a tutorial, you have likely encountered values that include a question mark. This question mark is declaring the value as an optional. So what exactly does that mean? Well, an optional is telling our compiler, “Hey, this may or may not actually have anything stored within it.” If the value is nil, the compiler will continue to run your application as it knows already that it wasn’t guaranteed that there was a value stored there.
Lets take a look at an example below:
In the example above, we have created two classes. We have a Developer class with a variable “computer” that is an optional Computer object. We then have a Computer class that has a single variable called “type” that is a string with the value of “Apple”.
Now say we wanted to create an instance of a Developer and force unwrap the computer value like below:
We have created a variable “austin” that is a Developer object. That means that we have an optional variable of computer that could either hold a Computer object with the variable of “type” in it, or this could be nil and not hold any value.
Since we have not assigned the variable austins.computer to our Computer class, this currently does not hold any value to it. The above code, if executed, would create a runtime error as we are force unwrapping the value by the use of the exclamation mark and it is nil.
To avoid this runtime error, we can utilize the use of the optional value and create an If Else statement to catch our nil value like below:
Running the above code will print out “Austin does not have a computer” since we have not yet created a value for the computer type in our austin variable.
If we want to assign the value of austin.computer to our Computer object we initially created, we can do so like the example below:
The above code will now execute the first print statement as we have assigned a value to the computer variable within austin.
As you can see, utilizing optional chaining is very easy and a good practice to utilize when creating your code. You can chain multiple optional variables together and if one of them fails by returning nil, the chain will fail without crashing your application and creating a runtime error. While simple, this is something that is often overlooked by developers when creating their code and then they must go through their codebase to locate the variable that is returning nil and crashing their application. With the use of optional chaining, you know that the compiler is already setup and aware that any variables that are not immediately declared may or may not hold a value.
I hope you enjoyed todays article and practice using optional chaining within your next Swift project! Please be sure to follow my page for future Swift, SwiftUI, and Apple articles!