Member-only story
Practical Reference Cycles in Swift
Don’t let this stop you!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12.0, and Swift 5.3
There are plenty of guides about reference cycles in Swift, and many of them will recommend you using [weak self]
to get out of trouble.
Prerequisites:
- You will be expected to be aware how to make a Single View Application, or a Playground to run Swift code
- Optional chaining is referred to within the article
Terminology:
Capture lists: The list of values that you want to remain unchanged at the time the closure is created
Capturing values: If you use external values inside your closure, Swift stores them alongside the closure. This way they persist even when the external part of the code no longer exists
Closure: A self-contained block of functionality that can be passed around
The project
Reference cycles are something that every iOS developer should be aware of, as they can cause memory leaks (which are memory allocations that are never released).