Bird
0
0

What does the ?. operator do in Swift when used with optionals?

easy📝 Conceptual Q11 of 15
Swift - Optionals
What does the ?. operator do in Swift when used with optionals?
AIt forces unwraps the optional and crashes if nil.
BIt converts the optional to a non-optional value automatically.
CIt safely accesses a property or method and returns nil if the optional is nil.
DIt assigns a default value if the optional is nil.
Step-by-Step Solution
Solution:
  1. Step 1: Understand optional chaining purpose

    The ?. operator is used to safely access properties or methods on an optional value.
  2. Step 2: Behavior when optional is nil

    If the optional is nil, the chaining stops and returns nil instead of crashing.
  3. Final Answer:

    It safely accesses a property or method and returns nil if the optional is nil. -> Option C
  4. Quick Check:

    Optional chaining = safe access with nil return [OK]
Quick Trick: Remember: ? stops if nil, no crash happens [OK]
Common Mistakes:
  • Confusing optional chaining with forced unwrapping
  • Thinking it unwraps optionals automatically
  • Assuming it provides default values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes