The word IBOutlet
is actually defined as nothing:
#define IBOutlet
Xcode just uses the presence of this word in your code for purposes of allowing you to make connections in Interface Builder. A declaration of a variable or a property as an IBOutlet
:
IBOutlet UIButton * button;
@property (...) IBOutlet UIButton * button;
therefore doesn't have any direct effect as far as ARC is concerned; it doesn't (although, conceivably, it could) translate into __weak
or anything like that. The word itself is entirely gone from your source by the time the compiler gets it.
On the other hand, the fact that this variable or property is an outlet does have a meaningful effect on how you need to think about the memory management.
The implicit storage qualifier for an object variable declaration like IBOutlet UIButton * button;
under ARC is __strong
, as you said -- any object assigned to the variable will be considered "owned". Under MRR, the declaration is just a pointer; assigning to has no effect on the reference count/ownership of the assigned object -- it acts in the same way as an assign
property.* So the meaning of the same ivar declaration changes between the two management systems.
Objects in a xib have owned/owner relationships that are formed by the view hierarchy; that is, parent views own their subviews. The top-level view in a xib is owned by the object known as File's Owner. This setup means that, generally speaking, your outlets to objects in an xib that are not top-level should be weak
(under ARC) or assign
(if a property under MRR). They are not owning relationships; they are essentially convenient indexes into the view list. This is Apple's recommendation:
...you don’t need strong references to objects lower down in the graph because they’re owned by their parents, and you should minimize the risk of creating strong reference cycles.
[...]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
. Outlets that you create should will [sic] therefore typically be weak
by default...
Your simple pointer IBOutlet
s, as I explained, acted -- for memory management purposes -- like weak
properties,** which means that they were doing the right thing. The same declaration becomes probably the wrong thing when compiled under ARC.
In summary: IBOutlet
does not translate into weak
, but it does change the meaning of the pointer. Since the default memory management semantics of IBOutlet UIButton * button;
change from "assign" under MRR to "owned" under ARC, and since IBOutlet
s should generally be non-owning, the presence of IBOutlet
does indeed imply that the pointer should be declared __weak
under ARC.†
*And similar to a weak
property -- the only difference there is that weak
pointers are set to nil
when the object is deallocated.
**Except for the auto-nil
part.
†Or, really, it should be a weak
property.