我有车和司机。他们相互引用。在汽车的 init() 中,我创建了一个驱动程序并将其分配给驱动程序成员。 driver 成员有一个 didSet 方法,该方法应该设置司机的车,从而将它们相互链接起来。
class GmDriver {
var car: GmCar! = nil
}
class GmCar {
var driver: GmDriver {
didSet {
driver.car = self
}
}
init() {
driver = GmDriver()
}
}
let myCar = GmCar()
println(myCar.driver.car) // nil
但是,didSet 永远不会触发。为什么?
最佳答案
The willSet and didSet observers of superclass properties are called when a property is set in a subclass initializer, after the superclass initializer has been called. They are not called while a class is setting its own properties, before the superclass initializer has been called.
关于swift - 在 Swift 中,从 init() 调用时 didSet 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29619846/