All about Optionals in Swift

sandeep kumar
4 min readAug 6, 2019
All about Optionals in Swift

Optionals in Swift are a type that can hold a value or nil value. We can represent Optionals using “?”

Optionals are just an enum that has either .nil or .some. They use Generics to pass the associated value.

How to Create Optionals:

  1. By direct method:

We can create optionals using “?”

This is the direct method- We can directly assign a variable as optional

All about Optionals in Swift

2. By initializing as Optional:

All about Optionals in Swift

Optional Bindings:

Optional binding is the process of unwrapping the optional values. There are several ways to unwrap the optionals”

Force unwrap

In this method, we unwrap the optional value without any nil value check. If the optional is nil, it will cause the app to crash. We can force unwrap optionals using “!”

All about Optionals in Swift

App Crash Example

All about Optionals in Swift

Unwrap by condition

For avoiding an app crash, we can use conditions like if,if__let, guard

Using if

In this scenario, we first check if the optional value is nil. If it isn’t nil, we forcefully unwrap the optional. Refer to these two scenarios for if conditions:

With value:

All about Optionals in Swift

Without value:

All about Optionals in Swift

Using If-Let

In some cases, one may not prefer to force unwrap the optionals. In this scenario, we can assign optionals into one variable and can use that variable in that scope using if- let.

With Value:

All about Optionals in Swift

without value:

All about Optionals in Swift

Using guard:

For avoiding the if-let code scenario and using a variable outside that scope, one can use the Guard. Guard also enables an early escape from the code.

All about Optionals in Swift

Guard can only be used inside a function

All about Optionals in Swift

Typecasting unwrap

We can assign an optional as “Any” and unwrap with respective typecase.

For example, we don’t know the type of the variable initially, and it may come as Int or String. In this case, we can use typecasting optionals. In the below screenshot, we applied the same variable as Int and String. We can unwrap these variables using typecasting.

All about Optionals in Swift
All about Optionals in Swift

Optional chaining

It is a process of querying or calling properties, methods, and subscripts on an optional that might be currently nil.

If the Optionals contain a value, the method or property call succeeds. Otherwise, it will be written as a nil value. Refer the screenshots below to understand how optional chaining works in a different scenario:

All about Optionals in Swift

Here, we are not assigned values to the class residence, ie options. So, when we try to access some property, it is shown as a nil value.

All about Optionals in Swift

In this screenshot, we assigned residence class so it is written as rooms value.

Note: We can apply multiple chains in one query itself.

Optionals Use Cases:

  • Can assign nil value

In some cases, we need to pass a NIL value. In this case, Optionals is the best practice. For example, if a user has a first name & a second name, and the second name is not mandatory, we can use Optional.

  • Values may fail

There are plenty of cases where the called value may fail. in this case, we can use optional instead of directly calling.

  • Values that are yet to appear

One may assign values that would come in later, using optionals.

  • Delegate value will not be assigned first
  • Property may or may not exist
  • Weak variables, where we can assign as nil

--

--