How to print the retain count of an object?

心已入冬 提交于 2019-12-25 08:55:03

问题


I am trying to print out the retain count of an object in the terminal using NSLog. Here is my code:

NSNumber *myInt=[[NSNumber alloc] initWithInteger: 100];
NSLog(@"myInt retain count=%d",[myInt retainCount]);

The result should be 1 but what I have got at the terminal is -1. I tried to use %u instead of %d and ended up getting 4294967295 as result. Does anyone know why this happens?


回答1:


Before @bbum gets here I get to say this:

Don't rely on -retainCount in your code

It doesn't give you answers you expect. It turns out that the answer -1 is correct here, the fact that you don't think it's correct is because the framework is doing something you don't know about behind your back. Use automatic reference counting (ARC), or if you must use manual retain/release just follow the memory management guidelines without using the -retainCount method.



来源:https://stackoverflow.com/questions/11507537/how-to-print-the-retain-count-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!