简体   繁体   English

为什么IBOutlet保留计数为2

[英]Why IBOutlet retain count is 2

Why IBOutlet retain count is 2 not just 1?? 为什么IBOutlet保留数是2而不仅仅是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? 为什么IBOutlet保留计数是2而不是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 . 如果框架希望将其设置为100,甚至没有理由,则没有理由不设置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. 加载NIB时,在第一种情况下,将指针直接分配给实例变量,在第二种情况下,将使用访问器分配实例变量。

  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 您应该查看Apple的文档- 此处链接

  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. 声明fooLabel变量以及Interface Builder nib文件的出口。

UILabel *fooLabel; as above without the outlet for Interface Builder. 如上,没有Interface Builder的插座。

@property (nonatomic, retain) IBOutlet UILabel *fooLabel; declares a property fooLabel and an outlet for your nib file. 为您的nib文件声明一个属性fooLabel和一个出口。 If you synthesize this property with synthesize fooLabel , it will create a getter and setter methods for the property. 如果使用synthesize fooLabel合成此属性,它将为该属性创建getter和setter方法。 The (retain) attribute tells the synthesized setter method to retain your new value before releasing the old one. (retain)属性告诉合成的setter方法在释放旧值之前保留新值。

1) Do not use retainCount to reason about "retain state" of object - When to use -retainCount? 1)不要使用retainCountretainCount对象的“保留状态”- 何时使用-retainCount?

2) In both cases outlet object will be retained because of KVC (in the first case it's "magic"). 2)在这两种情况下,都将由于KVC而保留出口对象(在第一种情况下,它是“魔术”)。 That means that in both cases you have to release it when you're done with it (eg in dealloc ). 这意味着在两种情况下,您都必须在使用dealloc其释放(例如,在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). 3)保证第二个代码段可以按预期工作,而第一个代码段的行为看起来像是依赖于我的实现(我找不到关于非属性ivars的有关KVC的清晰文档)。

Check your code carefully whether you are explicitly retaining the label([fooLabel retain]). 仔细检查您的代码是否明确保留了标签([fooLabel keep])。 If not, then don't release it twice. 如果没有,则不要释放它两次。 Release it only in dealloc. 仅在dealloc中释放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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