How to Unwrap Multiple Optionals with One If-Let in Swift

What do you do when you’ve got two (or more) optionals that you need to safely unwrap and work with?

Code examples

Suppose that you’ve got two arrays, both of which are optional. What I want to do right now is walk through a couple of scenarios where I unwrap them at the same time and print them to the console with a single if-let statement.

First, watch how it’s done to accomplish the goal of this article’s title. :]

Then, compare what you expected to be the print output, to the actual output to make sure your understanding of how the syntax works is complete.

Ready?

Scenario 1: Both arrays are initialized (non-nil)

Output:
[“Howdy!”, “Hello!”]
[“Hi!”, “Hey!”]

Breaking it down

The syntax for unwrapping multiple optionals with a single if-let block is straightforward. It’s if followed by a series of let [constantName] = [optionalName] statements, separated by commas.

The output of this one is pretty much what you’d expect, too. The string form of the arrays is printed to the console window in Xcode or in your Playground.

Scenario 2: One array is initialized (non-nil), and the other is nil

Now suppose that the arrays looked like this:

Question: What do you think will be printed?

1) [“Howdy!”, “Hello!”] and “nil”
2) Just [“Howdy!”, “Hello!”]
3) Nothing will be printed

If you chose door number 3, you’d be correct.

The if-let block between the {}’s is only executed if both greetings and salutations are non-nil.

Takeaway

Unwrapping multiple optionals with a single if-let statement is pretty easy: if followed by a series of let [constantName] = [optionalName] statements, separated by commas.

The behavior is similar to using the && operator in a normal if condition. It’s like saying “if this optional is non-nil AND this optional is non-nil, then do this”

If you expect to work with one of the optionals in the list even if the other is nil, you’re going to need to split that up into multiple if-lets: