I’m trying to observe a string in a Singleton from a view controller.
In my view controller I do this in viewDidLoad:
Singleton *singleton = [Singleton sharedSingleton];
[singleton addObserver:self
forKeyPath:@"testString"
options:NSKeyValueObservingOptionNew
context:nil];
..and this code is at the bottom of the view controller:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"recieved");
}
The testString, in the Singleton, is changed 5 seconds later, but the observeValueForKeyPath is not called.. What have I done wrong?
Use properties e.g.
self.testString = newStringinstead oftestString = newStringor KVO will not work.