Bird
0
0

Why do Swift closures capture variables by reference rather than by value?

hard📝 Conceptual Q10 of 15
iOS Swift - Swift Language Essentials
Why do Swift closures capture variables by reference rather than by value?
ATo allow closures to modify and keep updated state of captured variables
BBecause Swift does not support value types
CTo improve performance by copying variables
DTo prevent closures from accessing variables outside their scope
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable capture behavior

    Closures capture variables by reference so changes inside closure affect the original variable.
  2. Step 2: Identify reason for reference capture

    This allows closures to maintain and update state over time.
  3. Final Answer:

    To allow closures to modify and keep updated state of captured variables -> Option A
  4. Quick Check:

    Closures capture by reference for state updates [OK]
Quick Trick: Closures capture variables by reference to keep state updated [OK]
Common Mistakes:
  • Thinking Swift only has reference types
  • Believing capture is by value for performance
  • Assuming closures cannot access external variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes