Hint: Strong references pointing to each other cause leaks [OK]
Common Mistakes:
Thinking optional chaining causes compile error
Assuming ARC frees memory despite cycles
Believing nil assignment crashes program
5. You want to avoid a strong reference cycle between two classes Owner and Pet. Which is the best way to declare the reference from Pet to Owner to prevent memory leaks?
hard
A. Declare the reference as weak var owner: Owner?
B. Declare the reference as var owner: Owner
C. Declare the reference as unowned var owner: Owner?
D. Declare the reference as let owner: Owner
Solution
Step 1: Understand strong vs weak references
Strong references increase ARC count and can cause cycles; weak references do not increase ARC count.
Step 2: Choose weak reference to break cycle
Declaring owner as weak var owner: Owner? allows ARC to free objects properly by not increasing reference count.
Final Answer:
Declare the reference as weak var owner: Owner? -> Option A
Quick Check:
Use weak to avoid strong reference cycles [OK]
Hint: Use weak references to break cycles safely [OK]
Common Mistakes:
Using unowned incorrectly causing crashes
Using strong references causing memory leaks
Forgetting to make weak references optional
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.