Recall & Review
beginner
What is a strong reference cycle in Swift?
A strong reference cycle happens when two or more objects hold strong references to each other, preventing them from being deallocated and causing a memory leak.
Click to reveal answer
beginner
How does a weak reference help break a strong reference cycle?
A weak reference does not increase the reference count of an object. This allows one object to refer to another without preventing it from being deallocated, thus breaking the cycle.
Click to reveal answer
beginner
In Swift, how do you declare a weak reference?
You declare a weak reference by using the keyword
weak before the property declaration, like weak var delegate: SomeDelegate?.Click to reveal answer
intermediate
Why must weak references be optional in Swift?
Because the referenced object can be deallocated at any time, weak references must be optional to allow them to become nil automatically when the object is gone.
Click to reveal answer
intermediate
What happens if you use a strong reference instead of a weak reference in a delegate pattern?
Using a strong reference in a delegate pattern can create a strong reference cycle between the delegate and the delegator, causing both to stay in memory and leading to a memory leak.
Click to reveal answer
What keyword do you use in Swift to declare a weak reference?
✗ Incorrect
The keyword
weak declares a weak reference that does not increase the reference count.Why must weak references be optional in Swift?
✗ Incorrect
Weak references must be optional to allow them to become nil automatically when the referenced object is deallocated.
What problem does a strong reference cycle cause?
✗ Incorrect
Strong reference cycles cause memory leaks by keeping objects alive indefinitely.
Which of these is NOT a way to break a strong reference cycle?
✗ Incorrect
Using strong references everywhere causes cycles; it does not break them.
In a delegate pattern, what kind of reference should the delegate property usually have?
✗ Incorrect
Delegates are usually weak references to avoid strong reference cycles.
Explain what a strong reference cycle is and how weak references help to solve it in Swift.
Think about how objects keep each other alive and how weak references avoid that.
You got /5 concepts.
Describe how to declare a weak reference in Swift and why it must be optional.
Focus on the syntax and behavior of weak references.
You got /4 concepts.