Does IBOutlet __weak have?

Just start with ARC. Pre-ARC, I would just simply announce my exits, for example: IBOutlet UIButton *button; , so I do not save it or anything else. With ARC, not indicating weak or strong, implies strong.

So, if I do the same in ARC (i.e. IBOutlet UIButton *button; ), does this mean that this button is a strong link? or do I need him to explain this as weak?

In short, __weak? IBOutlet mean __weak?

+6
ios objective-c cocoa-touch automatic-ref-counting
Apr 25 2018-12-12T00:
source share
3 answers

The word IBOutlet is not actually defined as anything:

 #define IBOutlet 

Xcode simply uses the presence of this word in your code to allow you to create connections in Interface Builder. Declaring a variable or property as an IBOutlet :

 IBOutlet UIButton * button; @property (...) IBOutlet UIButton * button; 

therefore has no direct effect on ARC; he (though perhaps he couldn’t) translate to __weak or something like that. The word itself has completely disappeared from your source by the time the compiler gets it.

On the other hand, the fact that this variable or property is an output has a significant impact on how you need to think about memory management.

Implicit storage identifier for declaring an object variable, for example, IBOutlet UIButton * button; in ARC, __strong , as you said, any object assigned to a variable will be considered “owned”. According to the MRR, a declaration is just a pointer; assignment does not affect the reference count / ownership of the assigned object - it acts in the same way as the assign property. * Thus, the meaning of the same ivar declaration varies between the two management systems.

Objects in xib have an owner / owner relationship, which are formed by the presentation hierarchy; those. parent representations own their subzones. The top-level view in xib belongs to an object known as File Owner. This setting means that, generally speaking, your outputs for objects in xib that are not the top level should be weak (with ARC) or assign (if the property is in MRR). They do not own a relationship; they are essentially handy indexes on the list of views. This is Apple's recommendation :

... you do not need strong references to the objects located below on the chart, because they belong to their parents, and you should minimize the risk of creating strong reference cycles.

[...] The outputs should be weak , except for those that belong to the file owner, for the top-level objects in the nib file (or in iOS, in the storyboard script), which should be strong . Thus, the clips you create should be [sic], usually weak by default ...

Your simple IBOutlet s pointer, as I explained, acted - for memory management purposes - like the weak ** properties, which means they are doing the right thing. The same declaration is probably erroneous when compiling in ARC.

In short: IBOutlet does not translate to weak , but it changes the meaning of the pointer. Since the default memory management semantics is IBOutlet UIButton * button; changes from “assignment” according to the MRR to “owned” in the ARC, and since an IBOutlet usually does not have ownership, the presence of an IBOutlet does imply that the pointer is declared __weak under ARC.




* And it looks like the weak property - the only difference is that the weak pointers are set to nil when the object is freed.

** Except for auto- nil .

Or, indeed, it should be a weak property.

+16
Apr 29 '12 at 20:40
source share

No, IBOutlet is mostly split when compiling code. It is, however, an assistant for Xcode so that it can know what InterfaceBuilderOutlet is.

Basically this word will allow you to link an element in Interface Builder.

By default, it will still be strong (just imagine that the word is not there).

However, it is assumed that you configured it to be weak, because once something is connected with the interface designer, the THAT interface will contain links to it, so it makes no sense to have a double strong link, especially if this element is not intended to be stored in memory, when the interface was unloaded.

Read this question that you are looking for:

Should IBOutlets be strong or weak in ARC?

+4
Apr 25 '12 at 3:12
source share

The IBOutlet keyword is used only to associate an object with an element in Interface Builder. It has nothing to do with weak or strong or ARC or memory management.

+3
Apr 25 '12 at 3:11
source share



All Articles