IBOutlets应该在ARC下是强还是弱? [英] Should IBOutlets be strong or weak under ARC?
问题描述
我正在开发专为iOS 5使用ARC。应 IBOutlet
到 UIView
(和子类) strong
或 weak
?
I am developing exclusively for iOS 5 using ARC. Should IBOutlet
s to UIView
s (and subclasses) be strong
or weak
?
以下:
@property (nonatomic, weak) IBOutlet UIButton *button;
将摆脱所有这一切:
- (void)viewDidUnload
{
// ...
self.button = nil;
// ...
}
模板使用 strong
,因为从Interface Builder编辑器直接连接到标题时创建的自动生成的属性,但为什么? UIViewController
已有 strong
引用其视图
Are there any problems doing this? The templates are using strong
as are the automatically generated properties created when connecting directly to the header from the 'Interface Builder' editor, but why? The UIViewController
already has a strong
reference to its view
which retains its subviews.
推荐答案
目前Apple推荐的最佳做法是让IBOutlets强力 weak 特别需要避免保留周期。正如Johannes上面提到的,这是在WWDC 2015的实现UI设计UI界面构建器会话中被评论的,一个苹果工程师说:
The current recommended best practice from Apple is for IBOutlets to be strong unless weak is specifically needed to avoid a retain cycle. As Johannes mentioned above, this was commented on in the "Implementing UI Designs in Interface Builder" session from WWDC 2015 where an Apple Engineer said:
最后一个选项我想指出的是存储类型,它可以
要么强或弱。一般来说,你应该使你的插座
强,特别是如果你连接一个子视图的插座或
约束,并不总是会被视图
层次结构保留。你真正需要使一个插座弱的唯一时间是如果
你有一个自定义视图引用的东西支持视图
层次结构,一般不推荐。
And the last option I want to point out is the storage type, which can either be strong or weak. In general you should make your outlet strong, especially if you are connecting an outlet to a subview or to a constraint that's not always going to be retained by the view hierarchy. The only time you really need to make an outlet weak is if you have a custom view that references something back up the view hierarchy and in general that's not recommended.
我在Twitter上向IB团队的一位工程师问过这个问题,他确认 strong 应该是默认值,开发者文档已更新。
I asked about this on Twitter to an engineer on the IB team and he confirmed that strong should be the default and that the developer docs are being updated.
https://twitter.com/_danielhall/status/620716996326350848
https://twitter.com/_danielhall/status/620717252216623104
这篇关于IBOutlets应该在ARC下是强还是弱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!