Created
February 9, 2015 20:16
-
-
Save joshavant/a9af89c6eec83186b470 to your computer and use it in GitHub Desktop.
KVO Context Pattern
This file contains hidden or 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 *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