Bird
0
0

Why does optional chaining return an optional value even if the property accessed is non-optional?

hard📝 Conceptual Q10 of 15
Swift - Optionals
Why does optional chaining return an optional value even if the property accessed is non-optional?
ABecause Swift always wraps property values in optionals.
BBecause the chain might fail if any optional in the chain is nil, so result is optional.
CBecause optional chaining forces unwrapping internally.
DBecause the property is automatically converted to optional type.
Step-by-Step Solution
Solution:
  1. Step 1: Understand optional chaining behavior

    Optional chaining returns nil if any optional in the chain is nil, so the result must be optional.
  2. Step 2: Clarify why result is optional

    Even if the property itself is non-optional, the chain might fail, so Swift wraps the result in an optional.
  3. Final Answer:

    Because the chain might fail if any optional in the chain is nil, so result is optional. -> Option B
  4. Quick Check:

    Optional chaining returns optional result due to possible nil [OK]
Quick Trick: Optional chaining returns optional because chain can fail [OK]
Common Mistakes:
  • Thinking property is always optional
  • Assuming forced unwrap happens
  • Believing Swift converts all properties to optional

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes