O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

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

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

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