Just starting out with ARC. Pre-ARC, I would just simply declare my outlets as for example: IBOutlet UIButton *button; so I am not retaining it or anything. With ARC, not specifying weak or strong implies strong.
So if I do the same thing under ARC (i.e. IBOutlet UIButton *button;), does this mean button is a strong reference? or Do I have to explcility define it as weak?
In short, does IBOutlet imply __weak?
The word
IBOutletis actually defined as nothing: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:therefore doesn’t have any direct effect as far as ARC is concerned; it doesn’t (although, conceivably, it could) translate into
__weakor 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 anassignproperty.* 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) orassign(if a property under MRR). They are not owning relationships; they are essentially convenient indexes into the view list. This is Apple’s recommendation:Your simple pointer
IBOutlets, as I explained, acted — for memory management purposes — likeweakproperties,** which means that they were doing the right thing. The same declaration becomes probably the wrong thing when compiled under ARC.In summary:
IBOutletdoes not translate intoweak, but it does change the meaning of the pointer. Since the default memory management semantics ofIBOutlet UIButton * button;change from "assign" under MRR to "owned" under ARC, and sinceIBOutlets should generally be non-owning, the presence ofIBOutletdoes indeed imply that the pointer should be declared__weakunder ARC.†*And similar to a
weakproperty — the only difference there is thatweakpointers are set tonilwhen the object is deallocated.**Except for the auto-
nilpart.†Or, really, it should be a
weakproperty.