O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Strong reference cycles

A strong reference cycle is where the instances of two classes holds a strong reference to each other, preventing ARC from releasing either instance. Once again, we are not able to use a Playground for this example, so we need to create an Xcode project. In this project, 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 
    print("Initializing class1_Strong with name \(self.name)") 
  } 
  deinit { 
    print("Releasing class1_Strong with name \(self.name)") 
  } 
} 

As we can see from the code, MyClass1_Strong contains an instance of MyClass2_Strong, therefore, the instance of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required