NSString在Objective-C中保留计数 [英] NSString retain count in Objective-C

查看:90
本文介绍了NSString在Objective-C中保留计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

相应的结果是:

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

另外: 如果使用init NSMutableString实例; 它返回1;

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

推荐答案

http://whentouseretaincount.com

在编译时生成的不可变NSStrings是单例.因此,他们根本不做retain/release舞蹈.

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

NSString检测何时使用此类初始化,并直接直接返回字符串.您会发现该代码中alloc返回的对象与init...调用返回的对象不同.

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.

这篇关于NSString在Objective-C中保留计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆