Why retain count in negative value? [duplicate]

十年热恋 提交于 2020-01-21 11:39:07

问题


Possible Duplicate:
NSString retain Count

Is it possible that any object has its retain count in negative value ?

I have this code

NSString *str = [[NSString alloc] initWithString:@"Hello World"];
NSLog(@"String Retain Count: %i", [str retainCount]);

this will return the retain count -1.

Why this happened ?

also I have done like this

NSString *str = [[NSString alloc] init]

still its return negative value in retain count.

How this is happening ?

Please help to understand this thing!!!!!


回答1:


retainCount doesn't return the reference count of an object. - it returns unrelated nonsense.

(For performance reasons, immutable constant strings, when copied, return self. If you compare the pointer to @"" and [[NSString alloc] initWithString:@""], they will be equal.)



来源:https://stackoverflow.com/questions/14601270/why-retain-count-in-negative-value

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