NSString retain count in Objective-C [duplicate]

2019-03-07 00:18发布

This question already has an answer here:

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;

1条回答
Animai°情兽
2楼-- · 2019-03-07 01:18

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.

查看更多
登录 后发表回答