문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top