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)

1var greetings: [String]? = ["Howdy!", "Hello!"]
2var salutations: [String]? = ["Hi!", "Hey!"]
1if let g = greetings, let s = salutations {
2    print(g)
3    print(s)
4}

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:

1var greetings: [String]? = ["Howdy!", "Hello!"]
2var salutations: nil
1if let g = greetings, let s = salutations {
2    print(g)
3    print(s)
4}

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:

1if let g = greetings {
2    print(g)
3}
4
5if let s = salutations {
6    print(s)
7}
comments powered by Disqus