Does kvo happen when a weak property is set using ARC?

I am wondering if a property set as weak will be cleared through the arc, if it does not strongly refer, will any KVO be registered for the key path pointing to this fire with a weak property? This would be a very convenient feature, but I do not know if this will happen at present. Does anyone know if this is the case, and if this by default cannot be made to work?

+7
objective-c automatic-ref-counting key-value-observing
source share
2 answers

You cannot do this using ARC, but you can emulate this by associating an object with your iVar using objc_setAssociatedObject() , it will be freed when a 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 create on top of these very well-designed notifications using NSNotificationCenter or just custom blocks, depending on how much you rely on it for a particular ivar case or for many of them.

+9
source share

runtime modifiers are not integrated with KVO

therefore NO

0
source share

All Articles