注册 登录
编程论坛 IPhone(iOS)开发

新人,学习swift,但是发现有个析构的问题?

foxeer 发布于 2018-12-05 10:19, 2431 次点击
强引用循环,发现加了weak仍旧不起作用deinit中的打印没有运行,根本就没有进行析构,麻烦大佬看看,谢谢了
import Foundation

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
1 回复
#2
foxeer2018-12-05 10:25
我是实在没有办法了,百度都翻遍了,也没有相关问题解答,百度知道也发了,现在还没人回复,如果有大佬能解决的话,请尽快回复,再次万分感谢
1