RetainCount is -1 after alloc? [duplicate]

拥有回忆 提交于 2019-12-25 18:39:02

问题


Please don't mark Duplicate:- I have created the few lines

NSString *str = [[NSString alloc] init];
str = @"Test";
NSLog(@"%d",[str retainCount]);

and Output is -1 .Please explain.


回答1:


It is a dupe of the question I pointed to. It is -1 because you are printing UINT_MAX as a signed integer. The RC is -1 because that is a singleton string generated by the compiler, effectively, and is never allocated or deallocated.

This pattern:

    NSString *str = [[NSString alloc] init];
    str = @"Kashyap";

Makes no sense; you are allocating a string instance, assigning a reference to it to str in the first line, and then immediately overwriting that reference in the second line (and leaking the first string, if you aren't using ARC).

If there is a tutorial or book that is advocating that anti-pattern, please point it out.



来源:https://stackoverflow.com/questions/16728133/retaincount-is-1-after-alloc

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