IBOutlet是否暗示__weak? [英] Does IBOutlet imply __weak?

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

问题描述

刚开始使用ARC。预ARC,我只是简单地声明我的插座,例如: IBOutlet UIButton *按钮; 所以我不保留它或任何东西。使用ARC,不指定弱或强意味着强。



所以如果我在ARC下做同样的事情(即 IBOutlet UIButton * button; ),是一个强有力的参考?或者我必须明确定义为弱吗?



总之,IBOutlet是否暗示 __ weak?

解决方案

单词 IBOutlet 实际上定义为无:

  #define IBOutlet 

Xcode只是在你的代码中使用这个词的存在,以允许你在Interface Builder中进行连接。变量或属性声明为 IBOutlet

  IBOutlet UIButton *按钮; 
@property(...)IBOutlet UIButton * button;

因此就ARC而言没有任何直接效果;它不(虽然,可以想象,它可以)翻译成 __ weak 或任何类似的东西。在编译器获得它之前,这个单词本身已经从你的源头完全消失了。



另一方面,这个变量或属性是一个插口 对您如何需要考虑内存管理有意义的影响。



ARC下的对象变量声明的隐式存储限定符 IBOutlet UIButton * button; __ strong ,如你所说 - 分配给变量的任何对象都将被视为拥有。在MRR下,声明只是一个指针;分配对所分配对象的引用计数/所有权没有影响 - 它以与 assign 属性相同的方式运行。*所以同一个ivar声明的含义



xib中的对象具有由视图层次结构形成的所有者/所有者关系;也就是说,父视图拥有他们的子视图。 xib中的顶级视图由称为文件所有者的对象拥有。这个设置意味着,一般来说,你的xib中不是顶级对象的出口应该是 weak (在ARC下)或 assign (如果是MRR下的属性)。他们不拥有关系;它们本质上是进入视图列表的方便的索引。这是 Apple的推荐


...您不需要对图表中较低的对象进行强引用,因为它们他们的父母拥有,你应该尽量减少创建强大的参考周期的风险。



[...] code>,除了那些从文件的所有者到顶级对象的nib文件(或在iOS,一个故事板场景),应该是 strong 。默认情况下,您创建的商店出价通常为 weak ...



$ b b

您的简单指针 IBOutlet 如我解释的,为了内存管理的目的,如 weak 属性,**这意味着他们做正确的事情。



总结: IBOutlet 不会将翻译成 weak ,但它确实改变了指针的含义。由于 IBOutlet UIButton * button; 的默认内存管理语义从MRR下的assign改为ARC下的owned,因为 IBOutlet s一般应该是非拥有的, IBOutlet 的存在确实暗示指针应该声明 __ weak 在ARC之下。






weak 属性 - 唯一的区别是 weak 指针设置为 nil



**除了auto - nil 部分。



或者,它应该是 weak 属性。 p>

Just starting out with ARC. Pre-ARC, I would just simply declare my outlets as for example: IBOutlet UIButton *button; so I am not retaining it or anything. With ARC, not specifying weak or strong implies strong.

So if I do the same thing under ARC (i.e. IBOutlet UIButton *button;), does this mean button is a strong reference? or Do I have to explcility define it as weak?

In short, does IBOutlet imply __weak?

解决方案

The word IBOutlet is actually defined as nothing:

#define IBOutlet

Xcode just uses the presence of this word in your code for purposes of allowing you to make connections in Interface Builder. A declaration of a variable or a property as an IBOutlet:

IBOutlet UIButton * button;
@property (...) IBOutlet UIButton * button;

therefore doesn't have any direct effect as far as ARC is concerned; it doesn't (although, conceivably, it could) translate into __weak or anything like that. The word itself is entirely gone from your source by the time the compiler gets it.

On the other hand, the fact that this variable or property is an outlet does have a meaningful effect on how you need to think about the memory management.

The implicit storage qualifier for an object variable declaration like IBOutlet UIButton * button; under ARC is __strong, as you said -- any object assigned to the variable will be considered "owned". Under MRR, the declaration is just a pointer; assigning to has no effect on the reference count/ownership of the assigned object -- it acts in the same way as an assign property.* So the meaning of the same ivar declaration changes between the two management systems.

Objects in a xib have owned/owner relationships that are formed by the view hierarchy; that is, parent views own their subviews. The top-level view in a xib is owned by the object known as File's Owner. This setup means that, generally speaking, your outlets to objects in an xib that are not top-level should be weak (under ARC) or assign (if a property under MRR). They are not owning relationships; they are essentially convenient indexes into the view list. This is Apple's recommendation:

...you don’t need strong references to objects lower down in the graph because they’re owned by their parents, and you should minimize the risk of creating strong reference cycles.

[...]Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should will [sic] therefore typically be weak by default...

Your simple pointer IBOutlets, as I explained, acted -- for memory management purposes -- like weak properties,** which means that they were doing the right thing. The same declaration becomes probably the wrong thing when compiled under ARC.

In summary: IBOutlet does not translate into weak, but it does change the meaning of the pointer. Since the default memory management semantics of IBOutlet UIButton * button; change from "assign" under MRR to "owned" under ARC, and since IBOutlets should generally be non-owning, the presence of IBOutlet does indeed imply that the pointer should be declared __weak under ARC.


*And similar to a weak property -- the only difference there is that weak pointers are set to nil when the object is deallocated.

**Except for the auto-nil part.

Or, really, it should be a weak property.

这篇关于IBOutlet是否暗示__weak?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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