简体   繁体   中英

Why IBOutlet retain count is 2

Why IBOutlet retain count is 2 not just 1??

and what is the difference between

IBOutlet UILabel *fooLabel;

and

UILabel *fooLabel;


@property (nonatomic, retain) IBOutlet UILabel *fooLabel; 

Why IBOutlet retain count is 2 not just 1?

You don't care. No, honestly, you don't. This is precisely why people will tell you not to ever worry about retain counts. You can never guarantee that it will be any particular number you expect. Retain counts are Cocoa internal implementation details. There's no reason why it shouldn't be 100 if the framework wants it to be, or even UINT_MAX .

and what is the difference between

IBOutlet UILabel *fooLabel;

and

UILabel *fooLabel;

@property (nonatomic, retain) IBOutlet UILabel *fooLabel;

The first declares an instance variable that can act as an outlet. The second declares a property that can act as an outlet. When the NIB is loaded, in the first case the pointer is assigned directly to the instance variable and in the second, the accessor is used to assign the instance variable.

  1. The absolute retain count value is irrelevant to your own memory management practices. Do not rely on it to diagnose memory management issues. You should check out Apple's documentation - link here

  2. As for your second question, here's a quick overview

IBOutlet UILabel *fooLabel; declares a fooLabel variable along with an outlet for your Interface Builder nib file.

UILabel *fooLabel; as above without the outlet for Interface Builder.

@property (nonatomic, retain) IBOutlet UILabel *fooLabel; declares a property fooLabel and an outlet for your nib file. If you synthesize this property with synthesize fooLabel , it will create a getter and setter methods for the property. The (retain) attribute tells the synthesized setter method to retain your new value before releasing the old one.

1) Do not use retainCount to reason about "retain state" of object - When to use -retainCount?

2) In both cases outlet object will be retained because of KVC (in the first case it's "magic"). That means that in both cases you have to release it when you're done with it (eg in dealloc ).

3) Second snippet is guaranteed to work as intended, while behavior of the first one looks like implementation dependent to me (I can't find clear documentation on KVC for non-property ivars).

Check your code carefully whether you are explicitly retaining the label([fooLabel retain]). If not, then don't release it twice. Release it only in dealloc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM