Skip to content

Instantly share code, notes, and snippets.

@nicktoumpelis
Created August 13, 2014 09:16
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 nicktoumpelis/421490825e5490f8eda2 to your computer and use it in GitHub Desktop.
Save nicktoumpelis/421490825e5490f8eda2 to your computer and use it in GitHub Desktop.
KVO observation done right
static void * const kKVOContext = (void *)&kKVOContext;
@implementation Class
- (void)someMethod {
[super someMethod];
[observedObject addObserver:self
forKeyPath:@"keyPath"
options:NSKeyValueObservingOptionInitial
context:kKVOContext];
}
- (void)dealloc
{
[observedObject removeObserver:self forKeyPath:@"keyPath" context:kKVOContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (context != kKVOContext) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment