I know there are a few questions regarding with this already. And I know swift can only customise property setter and getter for computed properties. But I think this is the worst part of Swift. Because:
My code is like this:
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
}
}
The problem is the there is no _value like in Objective-c, the self.value will cause the value's willSet be called again.
Any idea? Thanks
willSet
does not define a setter. set
does.
var privateValue: Float = 0.0;
var value: Float {
set(newValue) {
if newValue != privateValue {
privateValue = newValue;
// do whatever I want
}
}
get {
return privateValue;
}
}
- All variables are exposed to outside. There is no private or public properties any more.
- There is no way to access the "internal" variable of the property like the objective-c, _variable
According to my understanding, privateValue
will not be accessible anywhere outside the local scope, which would resolve both of your complaints. (EDIT: might be wrong about accessibility; see comments.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With