O'Reilly logo

Learning Swift™ Programming by Jacob Schatz

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

By letting ARC do its thing, you can pretty much sit back and relax as ARC allocates and deallocates memory when it is needed. However, there are situations when Rose really never lets go—when you create a permanent bond between two classes. Consider the new Car and Driver classes. Notice that when you create a new Driver and new Car, your driver’s car will be nil and your car’s driver will be nil. You can see what I mean when I create a new car and driver:

var car = Car(name: "Ford")var driver = Driver()

Now you have a new car and new driver that ARC has reference counted. driver has a car that is nil, and car has a driver that is nil. Now you can assign car’s driver to car and driver’s car to car:

car.driver = driver ...

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