当归零弱引用在ARC下的Objective-C上变为零时,如何得到通知? [英] How to get notified when a zeroing weak reference becomes nil on Objective-C under ARC?

查看:79
本文介绍了当归零弱引用在ARC下的Objective-C上变为零时,如何得到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种机制可以使对象知道归零弱引用变为零?

Is there a mechanism which would allow an object to know that a zeroing weak reference turned nil?

例如,我有一个财产

@property (nonatomic, weak) MyClass *theObject;

当theObject取消分配并且属性变为nil时,我想收到通知.但是如何?当对象消失时,归零弱参考系统是否使用setter将属性设置为nil?

when theObject deallocates and the property turns nil I want to get notified. But how? Does the zeroing weak reference system use the setter to set the property to nil when the object goes away?

推荐答案

运行时仅将弱ivar _theObect设置为nil,而不调用自定义设置器.

The runtime just sets the weak ivar _theObect to nil, a custom setter is not called.

您可以做什么(如果您确实需要通知)

What you could do (if you really need the notification):

  • 定义本地观察者"类并在该类中实现dealloc,
  • 创建一个观察者对象并将其设置为_theObject的关联对象".

释放_theObject时,将释放并释放关联的对象(如果没有其他强大的引用).因此,调用它的dealloc方法.这是您的通知".

When _theObject is deallocated, the associated object is released and deallocated (if there are no other strong refereces to it). Therefore its dealloc method is called. This is your "notification".

(我正在电话上写这些,以后可以根据需要填写详细信息.)

(I'm writing this on the phone and can fill in the details later if necessary.)

这篇关于当归零弱引用在ARC下的Objective-C上变为零时,如何得到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆