在发送消息之前检查有效的委托对象 [英] Checking for a valid delegate object before sending it a message
问题描述
我正在尝试在Objective-C中实现代理模式,但是有时候调用代理时遇到了错误访问异常。看来这是由代表被释放造成的。苹果不建议保留代表。
I am trying to implement the delegate Pattern in Objective-C, however I am experiencing a Bad Access exception when invoking the delegate sometimes. It seems this is caused by the delegate being released. Apple does not recommend to retain delegates.
如何在尝试发送邮件之前如何检查我的委托是否仍然有效?
How can I check my delegate if is still valid before trying to send it a message?
推荐答案
如果有机会由设定者发布,那么您的设计有问题。您只应该将代理设置为比代理本身使用寿命更短的对象。例如,在子视图/控制器上设置代理是正确的,因为子视图/控制器的使用寿命比呼叫者的寿命短。
If there's a chance that the delegate will get released by the setter, then there's something wrong with your design. You should only set delegates on objects that have a shorter lifespan than the delegate itself. For example, setting a delegate on a subview/controller is fine, because the subview/controller has a shorter lifespan than the caller.
AFAIK没有可靠的方法检测对象是否已经被释放。
AFAIK, there is no reliable way to detect if an object has been released already.
这篇关于在发送消息之前检查有效的委托对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!