Bird
0
0

Why does the nil coalescing operator ?? only unwrap one level of optional in Swift?

hard📝 Conceptual Q10 of 15
Swift - Operators and Expressions
Why does the nil coalescing operator ?? only unwrap one level of optional in Swift?
ABecause Swift does not support nested optionals
BBecause it unwraps all nested optionals automatically
CBecause it is designed to unwrap only the first optional layer for safety
DBecause it throws an error if nested optionals exist
Step-by-Step Solution
Solution:
  1. Step 1: Recall behavior of ?? operator

    The operator unwraps only the first optional layer to avoid unexpected crashes and maintain control.
  2. Step 2: Understand Swift optional design

    Swift allows nested optionals but requires explicit unwrapping for each level to keep code safe and clear.
  3. Final Answer:

    Because it is designed to unwrap only the first optional layer for safety -> Option C
  4. Quick Check:

    ?? unwraps one optional level for safety = B [OK]
Quick Trick: ?? unwraps only one optional layer to avoid surprises [OK]
Common Mistakes:
  • Thinking ?? unwraps all nested optionals
  • Believing nested optionals are unsupported
  • Assuming runtime error on nested optionals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes