Created
February 18, 2009 18:35
-
-
Save stevestreza/66466 to your computer and use it in GitHub Desktop.
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
#import <Cocoa/Cocoa.h> | |
@interface SSKVOFix : NSObject { | |
NSMutableDictionary *kvo; | |
} | |
@end |
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
#import "SSKVOFix.h" | |
@implementation SSKVOFix | |
- (void)addObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{ | |
if(!kvo){ | |
kvo = [[NSMutableDictionary dictionary] retain]; | |
} | |
NSMutableArray *obj = [kvo objectForKey:keyPath]; | |
if(!obj){ | |
obj = [NSMutableArray array]; | |
[kvo setObject:obj forKey:keyPath]; | |
} | |
[obj addObject:anObserver]; | |
[super addObserver:anObserver forKeyPath:keyPath options:options context:context]; | |
} | |
- (void)removeObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath{ | |
if(kvo){ | |
NSMutableArray *obj = [kvo objectForKey:keyPath]; | |
NSUInteger index = [obj indexOfObject:anObserver]; | |
if(index != NSNotFound){ | |
[obj removeObjectAtIndex:index]; | |
[super removeObserver:anObserver forKeyPath:keyPath]; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment