7

I'm wondering if when a property that is set as weak gets cleared out via arc when it is not strongly referable, does any KVO registered for the key path pointing to that weak property fire? That would be a really handy feature but I'm unaware if this happens currently. Anyone know if it does, and if it doesn't by default can it be made to work?

Kevlar
  • 8,804
  • 9
  • 55
  • 81
  • 1
    See http://stackoverflow.com/questions/14957382/want-to-perform-action-when-weak-ivar-is-niled/14958114#14958114 – rmaddy Sep 15 '13 at 04:32

2 Answers2

11

You can't do that with ARC, but you can emulate this by associating an object with your iVar using objc_setAssociatedObject(), it will be deallocated when the weak variable dies.

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

You can build on top of that very elaborate notifiers, using NSNotificationCenter or just custom blocks, depending on how heavily you rely on that for a specific ivar case or for lots of them.

JTAS
  • 431
  • 4
  • 6
0

runtime modifiers are not integrated with KVO

so NO

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135