Skip to content

Instantly share code, notes, and snippets.

@quellish
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quellish/bc7ecfd885c97c60d4c2 to your computer and use it in GitHub Desktop.
Save quellish/bc7ecfd885c97c60d4c2 to your computer and use it in GitHub Desktop.
correct kvo observe value for key path
// in the initializer, or viewWillAppear,etc:
[self addObserver:self forKeyPath:keyPath options:options context:(__bridge void*)self];
// in dealloc, or viewDidDisappear, etc:
[self removeObserver:self forKeyPath:keyPath context:(__bridge void*)self];
- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context {
if ((__bridge id)context == self){
if ([[change valueForKey:NSKeyValueChangeNotificationIsPriorKey] isEqual:@YES]){
// willChange, etc.
} else {
// didChange, etc.
}
} else {
[super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment