NSString retainCount是2147483647 [英] NSString retainCount is 2147483647
问题描述
可能重复:
NSString保留计数
Objective C NSString *财产保留计数奇怪度
何时使用-retainCount?
为什么此代码显示保留值大于1?为什么它是2147483647?
NSString * data22 = [[NSString alloc] initWithString:@fsdfsfsdf];
int a = [data22 retainCount];
NSLog(@保留计数1 ==%d ====,a);
上述代码的输出是
保留计数1 == 2147483647 ====
因为你看了,所以是2147483647。不要看,这将是你期望的价值。
说真的。不要调用 retainCount
。永远不会。它没用。
为什么这是一个如此荒谬的数字是因为一个实现细节。 @...是一个常量字符串。 NSString
可以识别常量字符串并确定您的特定代码不需要第二个空间消耗常量不可变字符串的副本,因此返回已存在的常量字符串。 / p>
即单身人士其实例仅由编译器创建的类的类。保留/释放/自动释放/ retainCount 完全没有意义。
至于为什么它是2147483647,一张图片值一千话。或者,在这种情况下,31个设置位。
Possible Duplicates:
NSString retain Count
Objective C NSString* property retain count oddity
When to use -retainCount ?
Why does this code show the retain value greater than 1? And why is it 2147483647?
NSString *data22 = [[NSString alloc] initWithString:@"fsdfsfsdf"];
int a = [data22 retainCount];
NSLog(@"retain count 1== %d ====" ,a);
The output of the above code is
retain count 1== 2147483647 ====
It is 2147483647 because you looked. Don't look and it will be the value you expect.
Seriously. Don't call retainCount
. Not ever. It is useless.
Why it is such a ridiculous number is because of an implementation detail. @"..." is a constant string. NSString
can recognize constant strings and decides that your particular code has no need of a second space consuming copy of a constant immutable string and, thus, returns the already existing constant string.
I.e. a singleton. Of a class whose instances are only ever created by the compiler. For which retain/release/autorelease/retainCount are utterly devoid of meaning.
As for why it is 2147483647, a picture is worth a thousand words. Or, in this case, 31 set bits.
这篇关于NSString retainCount是2147483647的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!