I have been trying to follow up with the Swift official docs and have tried to go through Automatic Referencing count examples they provided. In their example they are trying to show how to avoid strong referencing cycle using weak reference.
import UIKit
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("\(name) is being deinitialized") }
}
class Apartment {
let unit: String
init(unit: String) { self.unit = unit }
weak var tenant: Person?
deinit { print("Apartment \(unit) is being deinitialized") }
}
var john: Person?
var unit4A: Apartment?
john = Person(name: "John Appleseed")
unit4A = Apartment(unit: "4A")
john!.apartment = unit4A
unit4A!.tenant = john
john = nil
print("Strong ref cycle")
As described in the doc when we assign nil to john reference it should invoke the deinit since there are no more strong ref assigned to it. But when I run the above example it does not print that statement. Not sure what's the issue. Is there some confusion regarding my understanding or something else is going on?