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
- if let and Shorthand Binding
- guard let for Early Exit
- Binding Multiple Optionals
- Optional Pattern in switch and for