-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
A strong reference cycle, or strong retain cycle, occur when two or more objects hold strong references to each other, preventing them from being deallocated. This happens because each object's reference count never reaches zero; their mutual references keep their reference counts above zero, preventing the deallocation of the instances. To avoid this, Swift provides mechanisms like weak and unowned references, which break the strong reference cycle by not increasing the reference count of the objects they refer to. Managing these references properly ensures proper memory usage and prevents memory leaks in applications.
Now let’s look at an example to see what strong reference cycles are. In this example, we start off by creating two classes named MyClass1_Strong and MyClass2_Strong with the following code:
class MyClass1_Strong {
var name = ""
var class2: MyClass2_Strong?
init(name: String) {
self.name = name
...