问题
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