paint-brush
Optionals in Swift for newbies.by@farhansyed
620 reads
620 reads

Optionals in Swift for newbies.

by Farhan SyedApril 12th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Optionals are basically a way to say <strong>“This may or may not have a&nbsp;value”</strong>.
featured image - Optionals in Swift for newbies.
Farhan Syed HackerNoon profile picture

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 😊.