Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBOutlets Strong or Weak - Does it Actually Make a Difference to Memory Management? (ARC)

I've been reading a lot recently on here and other sites about whether IBOutlets should be strong or weak. The official verdict is that they should be weak, except for when they reference top level xib objects.

This is fine.

However what I don't yet have a clear understanding of is why setting them to strong or weak actually would make a difference in terms of the app's memory management. In particular it seems since iOS6, viewDidUnload is not called on receiving a memory warning anymore, so in this sense the choice of weak or strong for IBOutlets would make no difference.

Am I right in thinking though that for view controllers no longer on the navigation stack (i.e after they have been loaded then popped), that any IBOutlets declared as weak would then be released ?

Thanks for any insights !

like image 884
GuybrushThreepwood Avatar asked Feb 17 '14 10:02

GuybrushThreepwood


People also ask

Should IBOutlets be weak or strong?

The official answer from Apple is that IBOutlets should be strong. The only case when an IBOutlet should be weak is to avoid a retain cycle. A strong reference cycle can result in memory leaks and app crashes.

Why are outlets weak IOS?

Outlets that you create will therefore typically be weak by default, because: Outlets that you create to, for example, subviews of a view controller's view or a window controller's window, are arbitrary references between objects that do not imply ownership.

What is Xcode IBOutlet?

The type qualifier IBOutlet is a tag applied to an property declaration so that the Interface Builder application can recognize the property as an outlet and synchronize the display and connection of it with Xcode. An outlet is declared as a weak reference ( weak ) to prevent strong reference cycles.


1 Answers

IBOutlets can declared weak because they will be created during XIB parsing and added to the UIView stack...so you don't need a strong reference to the object.

When you declare an IBOutlet weak you ensure that when the main UIView of UIViewcontroller disappear every subview will be deleted without any memory leak.

like image 88
IgnazioC Avatar answered Sep 23 '22 08:09

IgnazioC