Activity 1
Break a reference cycle
Create Parent and Child classes that strongly reference each other. Observe deinit, then change the back-reference to weak and compare.
Hint: A weak property must be declared with var and an optional type.
Swift Fundamentals
Understand object lifetime, strong reference cycles, and the careful use of weak and unowned references.
Before you begin
Learning stage
Automatic Reference Counting, or ARC, tracks strong references to class instances. An instance remains alive while at least one strong reference owns it, and its deinitializer runs after the final strong reference is removed. Value types do not use identity-based reference counting in the same way.
A memory leak can occur when objects or closures strongly retain one another. Use weak when a reference may become nil and should not own the target. Use unowned only when the target is guaranteed to outlive the reference; a wrong guarantee can crash. Capture lists let closures express these ownership choices.
Learning stage
The coordinator owns the screen, but the screen does not own its coordinator.
final class Coordinator {
var screen: Screen?
deinit { print("Coordinator released") }
}
final class Screen {
weak var coordinator: Coordinator?
deinit { print("Screen released") }
}
var coordinator: Coordinator? = Coordinator()
let screen = Screen()
coordinator?.screen = screen
screen.coordinator = coordinator
coordinator = nilLearning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Create Parent and Child classes that strongly reference each other. Observe deinit, then change the back-reference to weak and compare.
Hint: A weak property must be declared with var and an optional type.
Activity 2
For an immediately executed closure and a stored asynchronous callback, decide whether strong, weak, or unowned capture is appropriate and explain why.
Hint: Base the choice on ownership and lifetime, not habit.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
It becomes nil
Weak references are optional non-owning references that ARC clears automatically.
QUESTION 2
Two or more ownership paths keep one another strongly referenced so their counts never reach zero.
ARC cannot infer which strong ownership link should be broken.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: No. Choose capture semantics from the intended ownership and lifetime relationship.
Weak self is appropriate when the closure may outlive its owner and should not keep that owner alive. A closure owned by a short operation may need a strong capture to complete useful work. Blindly using weak can make required work disappear or spread optional handling without solving a real cycle.
Possible follow-ups
Learning stage
ARC automates reference counting, not ownership design. Model which object owns which lifetime, break cycles with justified non-owning references, and verify behavior rather than applying weak mechanically.