You must implement your own setter. In this setter, you know what the fuck has changed.
It is more optimized in this way. Much better and cheaper than doing KVO on yourself.
Your solution works, technically you can KVO yourself.
But imagine if you started using NSNotificationCenter to run methods in a class from within your class? It can be done? Yes. Should it be? Probably no. You may have a scenario where this is normal, but not in a purely object-oriented solution. To do this, you need to send a message to self .
Well, thatβs the same. Implement this:
- (void)setAFlag:(BOOL)flag;
For instance:
- (void)setAFlag:(BOOL)flag{ BOOL valueChanged = NO; if(_aFlag != flag){ valueChanged = YES; } _aFlag = flag; if(valueChanged) [self doSomethingWithTheNewValueOfFlag]; }
source share