Bird
0
0

How can you combine multiple optional binding with a where clause to unwrap and check values?

hard📝 Application Q9 of 15
Swift - Optionals

How can you combine multiple optional binding with a where clause to unwrap and check values?

let x: Int? = 5
let y: Int? = 10
if let a = x, let b = y, a < b {
    print("a is less than b")
} else {
    print("Condition failed")
}

What does this code do?

AUnwraps both optionals and checks if a is less than b before printing.
BBoth A and C are correct.
CPrints "Condition failed" if any optional is nil or condition false.
DOnly unwraps x and ignores y.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze multiple optional binding with condition

    Both x and y are unwrapped, then condition a < b is checked.
  2. Step 2: Understand else block behavior

    If any optional is nil or condition false, else block runs.
  3. Final Answer:

    Both A and C are correct. -> Option B
  4. Quick Check:

    Multiple binding with condition runs if all true [OK]
Quick Trick: Add conditions after bindings to filter values [OK]
Common Mistakes:
  • Thinking only optionals are unwrapped without condition
  • Ignoring else block possibility
  • Assuming partial unwrapping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes