为什么在下面的代码中保留计数显示为 2?
NSLog(@"retain count 1 for show detail -- %d",[showDetail retainCount]);
ChecklistDetail *detail = [appDelegates.arrayForChecklistDetails objectAtIndex:[sender tag]];
self.showDetail = detail;
NSLog(@"retain count 2 for show detail -- %d",[showDetail retainCount]);
这里,在上面的代码中,第一个nslog的输出是 “为显示详细信息保留计数 1 -- 0”, 这是正确的。但是,第二个 nslog 的输出如下 “显示详细信息的保留计数 2 -- 2”。
它的保留计数如何达到 2?
如果可以的话请帮助我......
NSLog(@"retain count 1 for show detail -- %d",[showDetail retainCount]);
ChecklistDetail *detail = [appDelegates.arrayForChecklistDetails objectAtIndex:[sender tag]];
self.showDetail = detail;
NSLog(@"retain count 2 for show detail -- %d",[showDetail retainCount]);
Here, in the above code, output of the first nslog is
"retain count 1 for show detail -- 0",
which is correct. However, the output of the second nslog comes as following
"retain count 2 for show detail -- 2".
How does its retain count go upto 2?
help me if u can....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为当你执行
self.showDetail =Detail;
时如果您的属性声明为“retain”,那么您将在保留计数上添加 1,
setter 会为您处理保留计数,当您分配给该属性时,setter 将增加您分配给该对象的对象的保留计数。财产。。类似地,当您将 nil 分配给属性时,它会释放它,即减少对象的保留计数。
because when you do
self.showDetail = detail;
you add one to the retain count if your property is declared with 'retain'
the setter handles the retain count for you, when you assign to the property the setter will increase the retain count for the object that you are assigning to the property. Similar when you assign nil to the property it will release it, i.e. decrement the retain count on the object.
要检查 iOS 中变量的保留计数和正确的内存管理,请使用 Instruments,并且不要检查保留计数工具,因为它不太可信。
要开始在 Xcode 4 中使用 Instruments,请按住左上角的“运行”按钮,然后按配置文件。这将使仪器出现。
然后有两个部分,即 Allocations 和 Leaks。在这里,您可以检查变量的内存分配和管理。
享受编码的乐趣...! :)
to check the Retain counts and proper memory management of variables in iOS, please use Instruments and do not check the Retain count facility as it is not very trustful.
To start using Instruments in Xcode 4, keep holding the Run button on the top left corner and then press on profile. This will make the Instruments come up.
Then there are two sections, which are Allocations and Leaks. Here, you can check the memory allocations and management of the variables.
Enjoy coding...! :)
我猜测您的 showDetail 属性具有保留语义。因此,当您执行此操作时:
合成的属性正在调用保留。另一种调用 setter 的方法可能会让这一点更清楚:
所以保留计数为 1。第二个保留由数组保存。
I'm guessing that your showDetail property has retain semantics. So when you do this:
the synthesised property is calling retain. The other way to call your setter might make this clearer:
So that's a retain count of 1. The second retain is being held by the array.