Search code examples
objective-cnsstringretaincount

NSString retain count in Objective-C


NSString* nsString=[[NSString alloc]initWithString:@"nsString"];
NSLog(@"nsString RetainCount:%li",[nsString retainCount]);

the corresponding result is:

 2013-03-04 11:18:03.291 ARC[655:303] nsString RetainCount:-1 

in addition: if use init a NSMutableString instance; it return 1;


Solution

  • http://whentouseretaincount.com

    Immutable NSStrings generated at compile time are singletons. Thus, they don't do the retain/release dance at all.

    NSString detects when it is initialized with such and just returns the string directly. You'd find that the object returned by alloc in that code is different than the one returned by the init... call.