为什么将计数保留在负值? [英] Why retain count in negative value?

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

问题描述


可能重复:

NSString保留计数

是否有可能任何对象都有保留计数为负值?

Is it possible that any object has its retain count in negative value ?

我有这段代码

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

这将返回保留计数 -1。

为什么会这样?

我也是这样做的

NSString *str = [[NSString alloc] init]

仍然保留计数中的返回负值。

这是怎么回事?

请帮助理解这件事!!!!!

Please help to understand this thing!!!!!

推荐答案

retainCount 不返回对象的引用计数。 - 它返回无关的废话。

retainCount doesn't return the reference count of an object. - it returns unrelated nonsense.

(出于性能原因,复制时不可变常量字符串返回 self 。如果将指针与 @进行比较 [[NSString alloc] initWithString:@] ,它们将是相等的。)

(For performance reasons, immutable constant strings, when copied, return self. If you compare the pointer to @"" and [[NSString alloc] initWithString:@""], they will be equal.)

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

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