NSString保留计数 [英] NSString retain Count

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

问题描述

几天前我在做一个项目,我必须看看什么是字符串的保留计数.

Just couple of days i was working on a project and i have to see what is retain count of a string.

但是它总是返回我"2147483647",为什么会这样?

But it always return me "2147483647", Why it is so?

签出此代码以自己检查.

Check out this code to check it yourself.

NSString *str = [[NSString alloc] initWithString:@"Hello World"];
NSLog(@"String Retain Count: %i", [str retainCount]); 

所以我的问题是为什么它不像其他对象一样返回1,为什么我得到"2147483647"

So my question is why it is not returning 1 like other objects return, why i am getting "2147483647"

先谢谢了.

推荐答案

编译器比您聪明.

看到@"Hello world"并想到啊!一个常量字符串!"

It sees @"Hello world" and thinks "Aha! A constant string!"

然后看到[[NSString alloc] initWithString:@"Hello world!"]并认为啊哈!一个用常量字符串创建的不可变对象!"

It then sees [[NSString alloc] initWithString:@"Hello world!"] and thinks "Aha! An immutable object created with a constant string!"

然后将它们都折叠为一个NSConstantString,该NSConstantStringretainCountUINT_MAX,因此它永远不会被释放.

It then collapses both of them down into a single NSConstantString, which has a retainCount of UINT_MAX, so that it can never be released.

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

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