Should IBOutlets be strong or weak under ARC?

后端 未结 11 2336
自闭症患者
自闭症患者 2020-11-22 01:52

I am developing exclusively for iOS 5 using ARC. Should IBOutlets to UIViews (and subclasses) be strong or weak?

11条回答
  •  日久生厌
    2020-11-22 02:31

    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. That's caused by the change in UIViewController that views are not unloaded anymore.

    • 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.
    • Since iOS 6, views are not unloaded anymore, but loaded once and then stick around as long as their controller is there. 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:

    • Using weak clearly states that the controller doesn't want ownership of the button.

    • But omitting weak doesn't hurt in iOS 6 without view unloading, and is shorter. Some may point out that is also faster, but I have yet to encounter an app that is too slow because of weak IBOutlets.

    • Not using weak may be perceived as an error.

    Bottom line: Since iOS 6 we can't get this wrong anymore as long as we don't use view unloading. Time to party. ;)

提交回复
热议问题