An -observeValueForKeyPath:ofObject:change:context: message was received but not handled
You have an object that got dealloc'ed and did not stop observing another object. Walk through all of your -addObserver...
calls and make sure they are matched with -removeObserver...
calls at least in the -dealloc
and possibly in the -viewDidUnload
depending on your application structure.
I saw this error when I sent the observeValueForKeyPath
method to super
, which I had not registered as an observer for the change. Apple docs say "Be sure to call the superclass's implementation [of observeValueForKeyPath
] if it implements it."
My fix was to change:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:kPropertyThatChanges]) {
...
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
to:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:kPropertyThatChanges]) {
...
}
}
In swift:
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == kPropertyThatChanges) {
}
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
To:
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == kPropertyThatChanges) {
}
}
I ran into this problem by accidentally passing the target in as the observer (instead of self) like so:
[self.someView addObserver:self.someView forKeyPath:@"key" options:0 context:nil];
The error message was not at all helpful in identifying this so just thought I'd post in case anyone else does the same thing.