Optionals in Swift for newbies.

Written by farhansyed | Published 2017/04/12
Tech Story Tags: ios-app-development | ios | mobile-app-development | mobile | swift

TLDRvia the TL;DR App

Optionals are basically a way to say “This may or may not have a value”.

A lot of newbies are unsure of what optionals are and how they work so I’ll hopefully try to explain this in an easy way to understand.

Pretend.

Your are building an app that requires users to sign up.

The sign up form has fields for first name, middle name, and last name.

As you see I’ve created simple constants like so:

<a href="https://medium.com/media/9eae3571b1ea7e955924c32e5bbd2dad/href">https://medium.com/media/9eae3571b1ea7e955924c32e5bbd2dad/href</a>

Each constant 100%, no doubt has a value in this case.

Print out as a full name like so:

<a href="https://medium.com/media/9feb5d9c69309dc5b0fa842119ccf97f/href">https://medium.com/media/9feb5d9c69309dc5b0fa842119ccf97f/href</a>

Some people don’t have a middle name, so that’s where optionals come in.

We can tell Swift middleName may or may not have a value by adding a ? next to String.

<a href="https://medium.com/media/b22f5eb91271ae51ab3a39cd45c99fe7/href">https://medium.com/media/b22f5eb91271ae51ab3a39cd45c99fe7/href</a>

Now notice when you print() fullName, you get some weird outcome like:

Steven Optional("Paul") Jobs

This is because we need to unwrap this optional.

Force Unwrapping.

This is pretty discouraged as it causes runtime crashes. Mainly because most beginner programmers just go with what Xcode suggests, which is to add a !.

! tells Swift, I know 100% certain this has a value, so you better be damn sure it contains a value otherwise expect a crash.

Since we do have a middleName, Paul let’s use ! in our print() statement.

<a href="https://medium.com/media/ac801ae2e3302b3ad2f36bd613de9fe6/href">https://medium.com/media/ac801ae2e3302b3ad2f36bd613de9fe6/href</a>

Now we will get this in our console.

Steven Paul Jobs

Though we do have the wanted outcome without the Optional(“Paul”).

It’s just not safe to do it this way.

Here’s why.

Change middleName like so:

<a href="https://medium.com/media/1aa84ebbef87f4f9cb386c3037576f04/href">https://medium.com/media/1aa84ebbef87f4f9cb386c3037576f04/href</a>

We simply made middleName equal to nil.

Now we get a fatal error:

unexpectedly found nil while unwrapping an Optional value

This is because we explicitly told Swift by putting ! your 100% certain that middleName has a value which in this case it doesn’t therefore it crashes.

Safely Unwrapping Optionals.

You can use if let statement to safely unwrap your optionals.

Simply like so:

<a href="https://medium.com/media/8ec75d8c6d9750dc826d49ce47f28ade/href">https://medium.com/media/8ec75d8c6d9750dc826d49ce47f28ade/href</a>

Basically this translates to:

If there is a middleName, take the value and store it in middle else do something.

Notice: we don’t use middleName, instead we use middle since that’s where the unwrapped value is stored.

Guards.

In a previous post I covered guards, and how they can be beneficial.

Here I’ll show you how to use guards to unwrap instead of using an if let statement.

<a href="https://medium.com/media/52c8fcfbca50d4ecb89c35fa35af7c8a/href">https://medium.com/media/52c8fcfbca50d4ecb89c35fa35af7c8a/href</a>

That’s it!

Now you safely unwrapped the optional without the chance of crashing.

I hope you now understand how optionals work and how to safely unwrap them.

If you have any questions or suggestions on what you’d like to read about next don’t hesitate to let me know 😊.


Published by HackerNoon on 2017/04/12