Your Guide to Using Optionals in Swift 5

With clear examples

Abboskhon Shukurullaev
Clean Software
3 min readJun 12, 2020

--

Photo by Ilya Pavlov on Unsplash

Intro

Optionals keep our data types nil, with the promise that they will be filled later. It is vitally important, when we need to parse data from JSON, but, we don’t have it right now.

When you access a force unwrapped optional that has a nil value, Xcode throws a runtime error telling that some nil value has been found while trying to read the property.

Hence, to avoid this unpleasant situation and we need to resort to optional binding. Optional bindings checks the existence of data types first, and then allows to start an operation. If data types remain nil for some reason, it simply ignores the step, avoiding the runtime error.

Let’s start

Optional binding is the process of attempting to assign an optional value to a constant in a conditional statement to see if the optional contains an underlying value. Optional binding unwraps the optional and, if it contains a value assign value to a constant as a non-optional type, making it safe to work with. This approach get rid of the need to continue working with the ambiguity of whether or not you are working with a value or with nil.

Example of using the if-let optional binding:

So now, if there’re no carrots, the console will have “There is no carrots” printed out.

Optional chaining

It is possible that optional value has its own optional properties, which may be illustrated as a box within a box. These are called nested optionals.

Guard

A guard statement declares a condition that must be true in order for the code after the guard statement to be executed. Using a guard statement for requirements improves the readability of your code, as opposed to using an if statement for the same check.

Now, let’s illustrate the difference of how if and guard work:

We can render the same code using more readable guard statement:

Difference between “if let” and “guard let”.

Guard with Optionals

Earlier in this article you’ve learned about optionals and how to perform work with optional binding if-let. When you unwrap optional using if-let syntax to bind it to a constant, the constant is valid within the braces.

Instead, you can use optional binding guard let and you will be able to interact with your code outside of the braces. Just as you compared guard with if statements, guard let is an opposite of if let as well.

The same situation with if let and guard let happens inside functions. Guard let makes all constants available throughout the rest of the function, while if let allows only within the control flow braces.

Closing thoughts

On this note we came to the end of optionals. I hope you have deep knowledge of optionals and can easily fly between types of optional bindings. If there are still some questions that bother you, feel free to leave a comment.

Interested in other relative topics? Feel free to visit my other relevant articles:

Thanks for reading!

--

--

Abboskhon Shukurullaev
Clean Software

iOS-developer. I have tons of knowledge to learn, and I am happy to share it during the period