编辑代码

class User {
    var name: String
    var phone: Phone?
    init(name: String) {
        self.name = name
    }
    deinit {
        print("\(name) deinit")
    }
}

class Phone {
    var name: String
    var owner: User?
    init(name: String) {
        self.name = name
    }
    deinit {
        print("\(name) deinit")
    }
}

var u: User?
var p: Phone?
u = User(name: "Linhuadong")
p = Phone(name: "iPhone13 Pro")

u!.phone = p
p!.owner = u

u = nil
p = nil