-(void)cf_removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;方法里,
NSMutableArray *observers = objc_getAssociatedObject(self, &KVO_observerArr);
if (!observers||observers.count <=0) {
return;
}
for (CFObserveInfo *info in observers) {
if ([info.keyPath isEqualToString:keyPath]) {
[observers removeObject:info];
// objc_setAssociatedObject(self, &KVO_observerArr, observers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
break;
}
}
其中observers指针是指向objc_setAssociatedObject方法调用的内存空间,操作observers指针时,即对其指向的内存空间做操作。我再for循环之后,重新调用objc_setAssociatedObject来获取观察者数组,并跟踪其值,是空数组。