I have a useless variable User * currentUser; which can be changed from any class. I want to keep it until NSUserDefaults for any changes.
Is it possible to use KVO for a global variable like this, or is there any other way to achieve a similar effect?
I added the application delegate as an observer for currentUser :
[self addObserver:self forKeyPath:@"currentUser" options:NSKeyValueObservingOptionNew context:nil];
-
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"currentUser"]) { NSDictionary * userDict = [currentUser dictionaryRepresantation]; [UserDefaults setObject:userDict forKey:@"USER_DATA"]; [UserDefaults synchronize]; } }
but it is not called.
source share