Created
November 7, 2011 15:14
-
-
Save breeno/1345253 to your computer and use it in GitHub Desktop.
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
#import <Foundation/Foundation.h> | |
@interface NSObject (RegisterObserverAdditions) | |
-(void) registerObserver:(NSObject *)observer | |
forKeyPath:(NSString *)keyPath | |
options:(NSKeyValueObservingOptions)options | |
context:(void *)context; | |
-(void) unregisterAllObservers; | |
@end |
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
#import "NSObject+RegisterObserverAdditions.h" | |
#import <objc/runtime.h> | |
#define kObservedKeyPaths @"__observedKeyPaths" | |
@interface NSObject() | |
-(NSMutableArray *) observedKeyPaths; | |
@end | |
@implementation NSObject (RegisterObserverAdditions) | |
-(void) registerObserver:(NSObject *)observer | |
forKeyPath:(NSString *)keyPath | |
options:(NSKeyValueObservingOptions)options | |
context:(void *)context | |
{ | |
if( [[self observedKeyPaths] containsObject: keyPath] ) | |
return; | |
[self addObserver: observer forKeyPath: keyPath options: options context: context]; | |
[[self observedKeyPaths] addObject: keyPath]; | |
} | |
-(void) unregisterAllObservers | |
{ | |
for( NSString *keyPath in [self observedKeyPaths] ) | |
{ | |
[self removeObserver: self forKeyPath: keyPath]; | |
} | |
[[self observedKeyPaths] removeAllObjects]; | |
} | |
#pragma mark- Internal Methods | |
-(NSMutableArray *) observedKeyPaths | |
{ | |
NSMutableArray *observered = objc_getAssociatedObject( self, kObservedKeyPaths); | |
if( !observered ) | |
{ | |
observered = [NSMutableArray array]; | |
objc_setAssociatedObject( self, kObservedKeyPaths, observered, OBJC_ASSOCIATION_RETAIN); | |
} | |
return observered; | |
} | |
@end | |
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
#import <Foundation/Foundation.h> | |
@interface NSObject (RegisterObserverAdditions) | |
-(void) registerObserver:(NSObject *)observer | |
forKeyPath:(NSString *)keyPath | |
options:(NSKeyValueObservingOptions)options | |
context:(void *)context; | |
-(void) unregisterAllObservers; | |
@end |
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
#import "NSObject+RegisterObserverAdditions.h" | |
#import <objc/runtime.h> | |
#define kObservedKeyPaths @"__observedKeyPaths" | |
@interface NSObject() | |
-(NSMutableArray *) observedKeyPaths; | |
@end | |
@implementation NSObject (RegisterObserverAdditions) | |
-(void) registerObserver:(NSObject *)observer | |
forKeyPath:(NSString *)keyPath | |
options:(NSKeyValueObservingOptions)options | |
context:(void *)context | |
{ | |
if( [[self observedKeyPaths] containsObject: keyPath] ) | |
return; | |
[self addObserver: observer forKeyPath: keyPath options: options context: context]; | |
[[self observedKeyPaths] addObject: keyPath]; | |
} | |
-(void) unregisterAllObservers | |
{ | |
for( NSString *keyPath in [self observedKeyPaths] ) | |
{ | |
[self removeObserver: self forKeyPath: keyPath]; | |
} | |
[[self observedKeyPaths] removeAllObjects]; | |
} | |
#pragma mark- Internal Methods | |
-(NSMutableArray *) observedKeyPaths | |
{ | |
NSMutableArray *observed = objc_getAssociatedObject( self, kObservedKeyPaths); | |
if( !observed ) | |
{ | |
observed = [NSMutableArray array]; | |
objc_setAssociatedObject( self, kObservedKeyPaths, observed, OBJC_ASSOCIATION_RETAIN); | |
} | |
return observed; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment