iOS没有提供原生方法去直接判断对象是否包含某个观察者,只能自己写个方法去判断
-(BOOL)targetObj:(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;
}