DISCLAIMER: This post is older than one year and maybe out of date.

Optionals

In Swift an Optional Type is considered a safe way to use variables. An Optional can represent a value that has been set or more importantly the lack of a set value as they default to nil when no value is present (yes that technically means they do always have a value). You tell Swift that you want to use an optional type by using ? after the variable type, so for example.

The important thing here for Objective-C developers is that unlike Objective-C where nil only works with reference types and nil means nothing, in Swift nil is a true value and it means there is no value present. If the value of an Optional is not nil then the value is said to be wrapped up in the variable and that means we need to unwrap it to gain access to the value(s) inside.

Let me quickly point out here that the opposite to an Optional type is a Non-Optional Type and those are things like Strings and Classes. A Non-Optional Type cannot be nil. The Swift compiler helps you out here but not allowing you to do something crazy like

The line above would cause a compile error, again this is Swift helping you make better code and being a better developer as I am sure you will quickly learn from the compiler hitting you over the knuckles a few times until you stop doing it. Just be glad it does not make you write 100 lines on the blackboard after school.

Return Types

We can also use Optional Types in return statements, yet another way for us to practice safe coding. Note the ? after Int in the return type. If this code seems a little alien to you still I recommend reading my post on Swift Functions – The Basics. The function below takes two parameters, a string to search for and the array in which to search. If a match is found the index of that match in the array will be returned otherwise we get nil back.

Upwrapping Optional Types

Unlike that sweater or socks you got for Christmas, you do actually want to unwrap the gift an Optional Type gave back to you. It is important that we still check that we have a value even if it is nil, this is easily done though and something you should get used to. In the example below we have an Optional Variable that gets defined and then we check to see if it has a value and do something accordingly. Something note worthy here is Optional Types can be checked as if they were booleans, although they are not. We can unwrap the Optional by assigning it to a variable at the same time as we test it, in this case with the let unwrappedType = myVariable line at the beginning of the if statement.

Optional Chaining

Another great feature of Optionals is the ability to chain them together allowing us to simplify our code and check many things at the same time. Unless you really like huge nested if statements that always entertain and amuse your team mates right?…Hands up those who have done it!

So with chaining we can take something like this where we require myVar1, myVar2 and myVar3 to all be happy to finally assign a value to myVariable that we defined at the beginning.

And turn it into a much more graceful

As we work through the chain of if statements, if at any point one of them returns nil then the chain stops and we do not continue. Take some time to think this through, it can seem a little confusing at first.

So to wrap up this overview of Optionals, use them in cases where you think you might get a missing value and want to ensure you have a ‘way out’ that does not involve a crash or unexpected behavior. It is also another good way to practice safe coding in Swift.

There is a LOT you can do with this power, here is a link to chaining Optionals for further reading if you are interested.

Share Button

Signup for my Free Swift Newsletter