Swift101: Optional

Waranchit Chaiwong
3 min readMar 20, 2021
Photo by learnappmaking

Optionals are Swift’s solution to the problem of representing both a value and the absence of a value nil.

Think of an optional as a box: it either contains exactly one value, or is empty. When it doesn’t contain a value, it’s said to contain nil

Photo by stackoverflow

As the example above, You can describe that variable’s contain integer or nil

When you define a variable as an optional type, that means the value is not a real type. Like the example above, errorCode isn’t an Integer type but it’s optional of Integer. If you would like to modify that value the result is Value of optional type 'Int?' must be unwrapped to a value of type Int. So, when you would like to use a real value, you should be unwrapping that variable. the solution of unwrapping has a 4 way:

Force Unwrapping

Optional Binding

Guard

Nil coalescing

Force Unwrapping

The exclamation mark after the variable name tells the compiler that you would like to look inside the box and take out the value.

when you try to unwrap an optional variable with force unwrapping but that doesn’t contain a value, the error occurs:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

How can you play it safe? You can use the if statement to check if the optional contains nil?. if it doesn’t, that means it contains a value and you can unwrap it.

Optional Binding

For this way, you don’t use exclamation marks for unwrapping value from the box.

You can define a constant variable on if statement.

  • If the optional contains a value, this value is unwrapped and stored in, or bound to, the constant, and then the if statement executes the if block.
  • If the optional doesn’t contain a value, then the if statement executes the else block.

Tips

it’s common practice to give the unwrapped constant the same name as the optional (shadowing that optional)

You can unwrap multiple values at the same time:

If you unwrap multiple values, It will only execute the if part of the statement when both optional contain a value.

Guard

guard statement is to check a condition and only continue executing a function if the condition is true.

The guard statement comprises guard followed by a condition that can include both Boolean expressions and optional binding, followed by else, followed by a block of code covered by the else will execute if the condition is false

Nil coalescing

You can use it when you want to get a value out of the optional no matter what and in the case of nil, you’ll use a default value.

Conclusion

  1. nil represents the absence of a value
  2. Non-optional variables and constants are never nil
  3. Optional are like boxes that can contain a value or be empty(nil)
  4. To work with the value inside an optional, you must first unwrap it from the optional.
  5. The safest ways to unwrap an optional’s value is by using optional binding or nil coalescing. Use forced unwrapping only when appropriate, as it could produce a runtime error
  6. You can guard let to bind an optional. If the binding fails. the compiler forces you to exist the current function

--

--