Why Force Unwrapping isn’t your Brightest Idea

Raquel Rahmey
Feb 23, 2017 · 4 min read

Force unwrapping is a technique that is used to unwrap an optional variable or constant in swift. Optional variable? What’s that?

Before we even get to how one unwraps an optional, we must first understand what an optional even is. An optional is a variable or constant that has one of two possibilities: it either contains a value, or it does not AKA it considered nil. Optionals are used in cases when we are not sure if the variable will have a value. To declare a variable optional we add a “?” after it.

Let’s say we wanted to create a variable of type String that contained our friend’s brother’s name. Oh no! Not everyone has a brother! Some people have only sisters or are an only child! Since we are not sure whether the variable brother will receive a value, we can create it as an optional.

The value of the variable “brother” is nil

By declaring it an optional, we avoid an initialization error, which we would have gotten if we tried to declare it as a String.

Let’s say, we meet a new friend, who tells us about their awesome brother Ralph. That’s amazing! Now we can set the value of our optional variable to Ralph.

Notice that the playground changed the value of the optional from nil to “Ralph”. Let’s try printing it!

Hold on. Look at what the compiler printed.

Even though we set a value to the optional string, the compiler still considers the variable to be an optional. Remember that after we declare brother to be “Ralph”, we can always reverse it by setting the variable back to nil.

IMPORTANT: Until runtime, the compiler will not know if the variable contains a value or not.

So how can we print the variable, without all the optional wrapping around it? In order to work with the value attached to the variable we have to unwrap the variable.

Let’s take imagine another scenario. You receive a gift from your friend. It is wrapped beautifully, but you have no idea what’ s inside. This is similar to what happens when the compiler interacts with an optional variable. It is receiving a gift but it has no idea what, if anything, is inside. Until you UNWRAP your gift, you can’t interact with it or use it in any way.

There are several ways to unwrap an optional variable.

The first, and safest way is through optional binding. This uses an if statement to check if an optional contains a value, and if it does, sets it to some temporary variable and performs some action on that variable.

There, that’s better. Note that here we are changing the brother variable to a constant. If we wanted to mutate the constant again, we would need to use “if var” as opposed to “if let”.

Another way we can unwrap the variable is known as force unwrapping. This is done by adding an exclamation point to the end of the variable name.

Why would anyone optionally bind when they can force unwrap? All you do is add an exclamation point — easy enough. However, be warned, this is considered by many dangerous, and a sign of a lazy or inexperienced coder.

Let’s go back to the example of the gift. Imagine receiving this gift, only to open it and find….NOTHING. That’s right, nothing. That wouldn’t make you very happy would it?

If the compiler attempts to unwrap a variable that does not contain a value the compiler doesn’t just get mad, it will CRASH due to a FATAL ERROR.

In conclusion, unless you are certain that the optional variable you are trying to unwrap contains a value, do not force unwrap, rather use optional binding or a guard statement to properly unwrap your variable. This will save you time, and create a better, more efficient program.

Make the compiler happy!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade