NSString保留计数 [英] NSString retain Count
问题描述
几天前我在做一个项目,我必须看看什么是字符串的保留计数.
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
,该NSConstantString
的retainCount
为UINT_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屋!