Created
August 13, 2014 09:16
-
-
Save nicktoumpelis/421490825e5490f8eda2 to your computer and use it in GitHub Desktop.
KVO observation done right
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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