An observer can be added to a property declared in a superclass, but not in the same class or in a class extension. You cannot declare the same property in two places in a function. The best solution I can come up with is something like this, when you have an optional closure that you evaluate to willSet, and you only assign something to this property when you want to observe the behavior.
maybe something like:
private var _willSetCallback: ((Int) -> (Bool))? var someProperty: Int { willSet { if let optionalBool = _willSetCallback?(newValue) { // do something } } } func someMethod() { self._willSetCallback = { newValue in return newValue > 0 } }
It is not particularly elegant, but can he more or less cope with the desired behavior?
source share