ARC下的IBOutlet和viewDidUnload [英] IBOutlet and viewDidUnload under ARC

查看:74
本文介绍了ARC下的IBOutlet和viewDidUnload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这方面有一个类似的问题,因为这里,但是我只想澄清一些在那里没有完全解释的内容。

There is a similar question to this on SO here, however I just want to clarify something that wasn't fully explained there.

我理解所有代表和出口 - 事实上任何对父对象的引用,做一个好公民,并考虑一分钟的对象图 - 应该归结为弱引用。由于归零的性质,在引用对象的保留计数达到零时,弱指针自动降至零,这是否意味着现在不需要在 viewDidUnload 中将IBOutlets设置为nil?

I understand that all delegates and outlets - in fact any reference to a "parent" object, to be a good citizen and think about the object graph for a minute - should be zeroing weak references. Due to the nature of zeroing weak pointers automatically dropping to nil on the referenced object's retain count reaching zero, does this mean that setting IBOutlets to nil in viewDidUnload is now unnecessary?

所以,如果我这样声明我的商店:

So, if I declare my outlet like so:

@property (nonatomic, weak) IBOutlet UILabel *myLabel;

以下代码是否有效?

- (void)viewDidUnload
{
    self.myLabel = nil;

    [super viewDidUnload];
}


推荐答案

做一些研究...

根据我的理解,弱类似于分配,因为它们都是弱引用。

As I understand it, weak is similar to assign, in that they're both weak references.

但是,assign不会创建归零引用。即如果有问题的对象被销毁,并且您访问该属性,您将获得 BAD_ACCESS_EXCEPTION

However, assign does not create a zeroing reference. i.e. if the object in question is destroyed, and you access that property, you WILL get a BAD_ACCESS_EXCEPTION.

弱当它所引用的对象被销毁时,属性会自动归零(= nil)。

Weak properties are automatically zeroed (= nil) when the object it is referencing is destroyed.

在这两种情况下,没有必要将property设置为nil,因为它没有有助于保留相关对象的计数。使用保留属性时必须这样做。

In both cases, it is not necessary to set property to nil, as it does not contribute to the retain count of the object in question. It is necessary when using retain properties.

显然,ARC还引入了一个新的强属性,它与保留属性相同?

Apparently, ARC also introduces a new "strong" property, which is the same as "retain"?

完成研究这里

这篇关于ARC下的IBOutlet和viewDidUnload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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