Skip to main content

What is optional chaining?

· One min read
Ace the iOS Interview
Aryaman Sharda
Sources & Resources

Main Source: đź”— Ace the iOS Interview

Additional Sources:

Further Reading:

TL/DR

Optional chaining is a convenient way of unwrapping multiple Optional properties sequentially. If any of the Optional values in theexpression resolve tonil, the entire expression will resolve tonil.

Consider the following expression involving multipleOptionalproperties:

user?.isAdmin?.isActive

If user or isAdmin or isActive is nil, the entire expression becomes nil. Otherwise, isActive will return the unwrapped value.

This is much more readable than using multiple guard and if let statements to break down a sequence of Optional values.