0Pricing
Swift Academy · Lesson

Binding Multiple Optionals

Unwrap several optionals in one condition.

Binding Several at Once

You can unwrap multiple optionals in one if let by separating them with commas. All must be non-nil for the body to run.

let a: Int? = 1
let b: Int? = 2
if let a, let b {
    print(a + b)
}

All or Nothing

If any optional in the list is nil, the entire block is skipped. It is an all-or-nothing unwrap.

let first: String? = "Ada"
let last: String? = nil
if let first, let last {
    print("\(first) \(last)")
} else {
    print("Missing a name")
}

All lessons in this course

  1. if let and Shorthand Binding
  2. guard let for Early Exit
  3. Binding Multiple Optionals
  4. Optional Pattern in switch and for
← Back to Swift Academy