Swift |optional, non-optional— pointers to remember

Shashank Mishra
Mac O’Clock
Published in
4 min readMay 9, 2020

Optionals are one of the most important features of Swift. We use it regularly in the Swift programming language. Also, it is the most common question (we can say the first question from the basics) in any iOS interview.

Photo by Maxwell Nelson on Unsplash

Optional is an enumeration with two values “none” & “some”. It is used to indicate that a variable(or constant) can either have some value or no value. By default, it will have a “nil” value. We make a variable optional using “?” & “!” during declaration.

*The right section is a playground result, showing the default value. “!” is used for special case optional that will be discussed later.

Unwrapping optional variable

As I have told earlier optional value is wrapped like some(value). In order to get the value, we need to unwrap it. There are many ways to get unwrapped value. Let's discuss one by one with examples.

#1 — Forced unwrapping If we are putting the “!” while accessing it, it is called forced unwrapping. It will unwrap the value.

We need to be sure that the variable is not nil while force wrapping it. Otherwise, there will be a run time error.

#2 — Optional binding is to check whether optional contains a value if it is not nil(nil check). The recommended way of unwrapping optional. Using this, we can write more predictable and less error-prone code.

Using “guard” statement
Using if-let

#3 — Nil coalescing is to unwrap optional with value or a default value if nil.

Non-optional allows us to declare variables without optional and without initial value but we have to assign a value before using it other compile-time error. It can’t be nil.

*No default value

We can’t assign nil to any non-optional variable. will get a compile-time error.

During initialization

There can be few cases where we can use non-optional too. However, we need to be sure that value won’t be nil in the lifetime of that variable.

Implicitly unwrapped optional(Special type of optional)

If we declare a variable(or constant) with the “!” sign. It looks similar to forced unwrapped but it is not. We define it during declaration time, not later.

Before Swift 4.0, it was used to unwrap the optional without adding “!” while using it.

It was a special type enum “ImplicitlyUnwrappedOptional”. However, It has been removed from Swift 4.0(SE-0054).

Even after using “!”, if we print “rollNumber”, it will give optional value. This is after Swift-4.0

Now if you implicitly unwrapped any variable it will work similarly to optional. There is still some usage for the same. My next blog will elaborate on this topic.

Additional Notes — In, Objective-C, we can return nil from the method but for objects only. For others, like struct, enumeration, Objective-C handles it in a different way(NSNotFound). Due to this, we have to be sure about the special type of value. Swift optional help us to indicate the absence of a value for any type.

--

--