当一个弱的属性被ARC设置为零时,kvo会发生吗?

Kev*_*lar 7 objective-c key-value-observing automatic-ref-counting

我想知道当一个被设置为弱的属性被强制引用时,是否通过弧清除了,是否有任何KVO注册了指向该弱属性的关键路径?这将是一个非常方便的功能,但我不知道目前是否发生这种情况.任何人都知道它是否存在,如果它没有默认可以使它工作?

小智 11

你不能用ARC做到这一点,但你可以通过将一个对象与你的iVar相关联来模拟objc_setAssociatedObject()它,当弱变量死亡时它将被释放.

@interface WeakObjectDeathNotifier : NSObject
@end
@implementation WeakObjectDeathNotifier
- (void)dealloc
{
    // the code that shall fire when the property will be set to nil
}
@end
Run Code Online (Sandbox Code Playgroud)

您可以在非常精细的通知器之上构建,使用NSNotificationCenter或仅使用自定义块,具体取决于您对特定ivar情况或其中许多情况的依赖程度.