Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should IBOutlet be weak or strong var? [duplicate]

I'm using Xcode 6.2 for iOS projects. In older versions of Xcode, when a connection was create for an IBOutlet, it was always weak storage. Now when I create connections, they are defaulted to strong. I leave it that way and don't notice any difference.

Which version of Xcode did the default change to strong and why?

like image 201
4thSpace Avatar asked Apr 02 '15 20:04

4thSpace


People also ask

Should IBOutlet be weak or strong?

It doesn't hurt to declare an outlet as strong. It's what Apple recommends and it ensures the outlet has a value for as long the view controller is alive.

Do outlets need to be weak?

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.

Is an IBOutlet a variable?

IBOutlet is a keyword which is added to a variable declaration. It's an indicator. It does not affect the declaration in any way. However, when the Interface Builder sees it, it will allows a programmer to set this variable through the “outlet” mechanism inside Interface Builder.

How do you declare IBOutlet property?

If you have a property defined that you want to make accessible to your storyboards, just add the @IBOutlet attribute before your property. Similarly with @IBAction to connect storyboard actions back to code. class MyViewController: UIViewController { @IBOutlet weak var likeButton: UIButton?


2 Answers

Yes, previously outlets should generally be weak but Apple has changed that. Now they recommend to use strong outlets in the WWDC 2015 session Implementing UI Designs in Interface Builder. The only reason why it might be weak is retain cycle.

like image 154
beryllium Avatar answered Sep 19 '22 17:09

beryllium


It doesn't matter weak or strong the outlet is in most cases. You just be sure that you don't have strong reference cycles.

Subviews shouldn't have strong outlets to their superviews and view shouldn't have strong outlet to its controller, because superview already has strong reference to its subviews and controller has strong reference to its view.

like image 43
Dmitry Korotchenkov Avatar answered Sep 16 '22 17:09

Dmitry Korotchenkov