Skip to content

Instantly share code, notes, and snippets.

@joshavant
Created February 9, 2015 20:16
Show Gist options
  • Select an option

  • Save joshavant/a9af89c6eec83186b470 to your computer and use it in GitHub Desktop.

Select an option

Save joshavant/a9af89c6eec83186b470 to your computer and use it in GitHub Desktop.
KVO Context Pattern
static void *kFOOClassKVOContext = &kFOOClassKVOContext;
@implementation FOOClass
- (void)addKVOObservers
{
[self.property addObserver:self forKeyPath:@"someKeyPath" options:0 context:kFOOClassKVOContext];
}
- (void)removeKVOObservers
{
[self.property removeObserver:self forKeyPath:@"someKeyPath" context:kFOOClassKVOContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == &kFOOClassKVOContext) {
// do stuff
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment