<分区>
我知道已经有一些关于此的问题。而且我知道 swift 只能为计算属性自定义属性 setter 和 getter。但我认为这是 Swift 最糟糕的部分。因为:
- 所有变量都暴露在外。不再有私有(private)或公共(public)属性(property)。
- 无法像 objective-c _variable 那样访问属性的“内部”变量
我的代码是这样的:
var value : Float = 0.0 {
willSet {
setValue(newValue, animated: false)
}
}
func setValue(newValue:Float, animated:Bool) {
if(newValue != self.value) {
// TODO: this will cause problem because I there is no alternative way like Objective-c to access _value
self.value = ....
// do whatever I want
}
}
问题是没有像 Objective-c 中那样的 _value,self.value 会导致值的 willSet 再次被调用。
有什么想法吗?谢谢