由于iOS原生方法不支持判断对象KVO是否含有某个观察者,所以自己写了个投机取巧的方法去判断
-(BOOL)isObj:(NSObject *)obj hasObserverKey:(NSString *)keyPath{
if (obj == nil) {
return NO;
}
if (obj.observationInfo == nil) {
return NO;
}
NSObject *observances = [((__bridge NSObject*)(obj.observationInfo))valueForKeyPath:@"observances"];
if ([observances isKindOfClass:[NSArray class]]) {
for (NSObject *observance in (NSArray *)observances) {
NSString *inKeyPath = [NSString stringWithFormat:@"Key path: %@",keyPath];
NSString *objString = [NSString stringWithFormat:@"%@",observance];
if ([objString containsString:inKeyPath]) {
return YES;
}
}
}
return NO;
}