Skip to content

Instantly share code, notes, and snippets.

@stevestreza
Created February 18, 2009 18:35
Show Gist options
  • Save stevestreza/66466 to your computer and use it in GitHub Desktop.
Save stevestreza/66466 to your computer and use it in GitHub Desktop.
#import <Cocoa/Cocoa.h>
@interface SSKVOFix : NSObject {
NSMutableDictionary *kvo;
}
@end
#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