How do I overwrite the setter of stored property in Swift?
In Obj-C, I can overwrite its setter, but Swift doesn\'t seem to be happy about getter/setters being used
If you don't want to use didSet, which has the problem that the property's value is temporarily wrong, you should wrap a computed property around it.
private var _foo:Int = 0
var foo:Int {
get {
return _foo
}
set {
if(newValue > 999) {
_foo = 999
} else {
_foo = newValue
}
}
}
Or:
private var _foo:Int = 0
var foo:Int {
get {
return _foo
}
set {
guard newValue <= 999 else {
_foo = 999
return
}
_foo = newValue
}
}