问题
苹果的文件明确指出:
当属性第一次初始化时,不会调用willSet和didSet观察器。只有当属性的值在初始化上下文之外设置时才会调用它们。
是否有可能在初始化期间强制调用这些?
Why?
假设我有这样一个类
class SomeClass {
var someProperty: AnyObject {
didSet {
doStuff()
}
}
init(someProperty: AnyObject) {
self.someProperty = someProperty
doStuff()
}
func doStuff() {
// do stuff now that someProperty is set
}
}
我创建了方法doStuff,以使处理调用更简洁,但我宁愿只处理didSet函数中的属性。有没有办法在初始化期间强制调用这个?
更新
我决定删除类的方便初始化器,并强制您在初始化后设置属性。这让我知道didSet总是会被调用。我还没有决定这是否是更好的整体,但它很适合我的情况。
创建一个自己的set-Method,并在init-Method中使用它:
class SomeClass {
var someProperty: AnyObject! {
didSet {
//do some Stuff
}
}
init(someProperty: AnyObject) {
setSomeProperty(someProperty)
}
func setSomeProperty(newValue:AnyObject) {
self.someProperty = newValue
}
}
By declaring someProperty as type: AnyObject! (an implicitly
unwrapped optional), you allow self to fully initialize without
someProperty being set. When you call
setSomeProperty(someProperty) you're calling an equivalent of
self.setSomeProperty(someProperty). Normally you wouldn't be able to
do this because self hasn't been fully initialized. Since
someProperty doesn't require initialization and you are calling a
method dependent on self, Swift leaves the initialization context and
didSet will run.