When Apple introduced Swift they talked about three main things they set out to achieve: safe, modern and powerful. One such power feature is called optional. Most language features exist to solve a problem and naturally optional was created to solve a problem. Let’s explore an example to understand its use and purpose. Assume that you have an array containing apartment numbers that violated their rental agreement (oh yeah, they are in trouble). We need to check if an apartment number exists in that array so we can send them a notice.

let aptNumbers = ["101","202","303","404"]

We can write a very simple function to see if we can find a an apartment number within the array.

The above function takes in two parameters. A string and an array of strings. The first parameter is the apartment number we are looking for and the second parameter is an array of all apartment numbers. Then within the function we iterate over our array storing each apartment number in a temporary variable called tempAptNumber. We then compare the values and if we find a match we return it. However, the above function is incomplete because we are only returning a value when one is found. What happens when we don’t find a value? We must return something. Ideally, we should return a nil but we specified String as our return type. One solution would be to return an empty string:

The above solution is a failure because we should be able to use an if statement such as:

if findApt("505", aptNumbers) {
sendNotice()
}

However, that if statement will never get executed because it always returns a String value. Moreover, it does not evaluate to a boolean expression. We could compare against an empty string but that defeats the purpose. It would be nice to return a nil when the apartment number is not found in the array. Changing the last line to return nil will give us a compiler error because we set our return value to be a String. This is where optionals come in very handy.

An optional can have two states, a value or a nil. To declare an optional, simply suffix it with a ?. Let’s rewrite our function above to use an optional.

Instead of sending a generic notice, let’s assume we needed to use a function sendNoticeTo(apt: Int), where we are sending a notice to a specific apartment number. In that case, we cannot simply pass the constant culprit.

if culprit {
sendNoticeTo(culprit);
}

Firstly, because culprit is a string and secondly because it is an optional. As I mentioned above that optionals can either contain a value or nil. When we created the constant culprit it was implicitly defined as an optional too since that is the return type of our function.

It is important to understand that String and String? are not the same. The former can only contain a string whereas the later can contain either a string or a nil. So how do we convert the result of our function to the value of type String?. You can instruct the optional to provide you with a value if there is one.

let culprit = findApt("404",aptNumbers)
if culprit {
sendNoticeTo(culprit!) // Note the use of the ! operator to unwrap the value to String
}

We are not quite there yet because we need to send an Int to our sendNoticeTo function. Besides, we can also combine the two above statements into one:

When used as an if let statement the constant culprit is assigned the actual value of type String, which is the same as unwrapping the value using a bang ! operator. Now that culprit is of type String we can call the toInt() function on it. There’s still one issue we haven’t addressed. The function toInt() returns Int? which is also an optional. Now we could nest if statements.

The above ties in very nicely because we chain the call to the function toInt by using the ? operator. Since toInt also returns an optional we can check to see if we received a valid apartment number and then call the function sendNoticeTo else nothing will happen.

Conclusion

Optionals help save a lot of time and make our code readable and efficient. It might take a little getting used to but once you get the hang of it you would hate to have code lying around that explicitly checks for nils or sentinel values.

Excellent tutorial – the clearest one I’ve seen on optionals. I followed it along in a playground & your explanations together with the error messages in the playground have given me a much better picture of how optionals work & when they might be useful.

Each is designed by our faculty of tech professionals to guide even a coding beginner to becoming a job-ready software developer armed with a professional portfolio of real-world projects. Try one out today with our free seven-day trial, and see if software development is for you.