本文翻译自:Should IBOutlets be strong or weak under ARC?
I am developing exclusively for iOS 5 using ARC. 我正在使用ARC专为iOS 5开发。 Should IBOutlet
s to UIView
s (and subclasses) be strong
or weak
? IBOutlet
到UIView
(和子类)应该strong
还是weak
?
The following: 下列:
@property (nonatomic, weak) IBOutlet UIButton *button;
Would get rid of all of this: 将摆脱所有这些:
- (void)viewDidUnload
{
// ...
self.button = nil;
// ...
}
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? 从“接口生成器”编辑器直接连接到标题时,模板使用的功能很strong
就像自动生成的属性一样,但是为什么? The UIViewController
already has a strong
reference to its view
which retains its subviews. UIViewController
已经对其view
进行了strong
引用,并保留了其子视图。
#1楼
参考:https://stackoom.com/question/WDWH/在ARC下-IBOutlets应该强还是弱
#2楼
I think that most important information is: Elements in xib are automatically in subviews of view. 我认为最重要的信息是:xib中的元素自动出现在视图的子视图中。 Subviews is NSArray. 子视图是NSArray。 NSArray owns it's elements. NSArray拥有它的元素。 etc have strong pointers on them. 等对他们有很强的指导意义。 So in most cases you don't want to create another strong pointer (IBOutlet) 因此,在大多数情况下,您不想创建另一个强指针(IBOutlet)
And with ARC you don't need to do anything in viewDidUnload
借助ARC,您无需在viewDidUnload
执行任何操作
#3楼
While the documentation recommends using weak
on properties for subviews, since iOS 6 it seems to be fine to use strong
(the default ownership qualifier) instead. 尽管文档建议对子视图使用weak
属性,但是从iOS 6开始,最好使用strong
(默认所有权限定符)。 That's caused by the change in UIViewController
that views are not unloaded anymore. 这是由于UIViewController
中的更改导致不再卸载视图而引起的。
- Before iOS 6, if you kept strong links to subviews of the controller's view around, if the view controller's main view got unloaded, those would hold onto the subviews as long as the view controller is around. 在iOS 6之前,如果您保持指向控制器视图子视图的强链接,则如果视图控制器的主视图被卸载,只要视图控制器在周围,这些视图就会保留在子视图中。
- Since iOS 6, views are not unloaded anymore, but loaded once and then stick around as long as their controller is there. 从iOS 6开始,视图不再被卸载,而是被加载一次,然后只要它们的控制器在那里就一直存在。 So strong properties won't matter. 因此,强大的属性无关紧要。 They also won't create strong reference cycles, since they point down the strong reference graph. 它们也不会创建强参考周期,因为它们指向强参考图。
That said, I am torn between using 也就是说,我在使用之间感到恐惧
@property (nonatomic, weak) IBOutlet UIButton *button;
and 和
@property (nonatomic) IBOutlet UIButton *button;
in iOS 6 and after: 在iOS 6及更高版本中:
Using
weak
clearly states that the controller doesn't want ownership of the button. 明确使用weak
表示控制器不希望拥有按钮。But omitting
weak
doesn't hurt in iOS 6 without view unloading, and is shorter. 但是,在不卸载视图的情况下,忽略weak
不会对iOS 6造成伤害,并且更短。 Some may point out that is also faster, but I have yet to encounter an app that is too slow because ofweak
IBOutlet
s. 有人可能会指出这也更快,但是由于IBOutlet
weak
,我还没有遇到一个太慢的应用程序。Not using
weak
may be perceived as an error. 不使用weak
可被视为错误。
Bottom line: Since iOS 6 we can't get this wrong anymore as long as we don't use view unloading. 底线:从iOS 6开始,只要我们不使用视图卸载,就不会再犯此错误。 Time to party. 参加聚会的时间。 ;) ;)
#4楼
请注意, IBOutletCollection
应该为@property (strong, nonatomic)
。
#5楼
From WWDC 2015 there is a session on Implementing UI Designs in Interface Builder . 从WWDC 2015开始,有一个关于在Interface Builder中实现UI设计的会议。 Around the 32min mark he says that you always want to make your @IBOutlet
strong . 他说,在32 @IBOutlet
左右,您总是想使@IBOutlet
更强 。
#6楼
The current recommended best practice from Apple is for IBOutlets to be strong unless weak is specifically needed to avoid a retain cycle. 苹果公司目前建议的最佳做法是使IBOutlets 强,除非特别需要弱化以避免保留周期。 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: 正如Johannes所述,在WWDC 2015的“在Interface Builder中实现UI设计”会议中对此进行了评论,其中苹果工程师说:
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. 真正需要弱化出口的唯一情况是,如果您有一个自定义视图,该视图引用了备份视图层次结构的内容,通常不建议这样做。
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. 我在Twitter上向IB团队的一名工程师询问了此问题,他确认将默认设置为“ 强” ,并且正在更新开发人员文档。
https://twitter.com/_danielhall/status/620716996326350848 https://twitter.com/_danielhall/status/620717252216623104 https://twitter.com/_danielhall/status/620716996326350848 https://twitter.com/_danielhall/status/620717252216623104