Strong Reference Cycles

Create a new command-line tool named CyclicalAssets. Add a new file to your project named Person.swift and insert the following definition of the Person class.

Listing 24.1 Defining the Person class (Person.swift)

import Foundation

class Person: CustomStringConvertible {
    let name: String

    var description: String {
        return "Person(\(name))"
    }

    init(name: String) {
        self.name = name
    }

    deinit {
        print("\(self) is being deallocated")
    }
}

The Person class has a single property that you set in its initializer. It conforms to the CustomStringConvertible protocol by implementing the description computed property. You add an implementation of deinit so you can see when a person is being deallocated – i.e., when its ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.