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 ...

Get Learning Swift™ Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.