NSString保留计数-1 [英] NSString retain count -1
问题描述
我想在 didFinishLaunchingWithOptions中的
方法 AppDelegate
类中打印 NSString
的保留计数
I want to print retain count of NSString
in AppDelegate
class in didFinishLaunchingWithOptions
method
NSString *str = [[NSString alloc] init];
NSLog(@"str retain count %d",[str retainCount]);
但它总是给-1而不是+1 ....为什么???
but it always gives -1 instead of +1....why ???
推荐答案
NSUIntegerMax
表示返回的对象是不朽的。这曾经在文档中,但后来被删除了(因为非常不鼓励 -retainCount
)。
NSUIntegerMax
means the returned object is immortal. This used to be in the docs, but has since been removed (because -retainCount
is very discouraged).
在MRC中,有些人实际上会覆盖 -retainCount
以防止他们的单身人士被解除分配。
In MRC, some people would actually override -retainCount
to prevent their singletons from ever being deallocated.
在这种情况下,它是 [[NSString alloc] init]
的逻辑优化,用于返回常量/不朽,而不是为每个请求创建一个空字符串。当然,您不应该依赖程序中的这些细节/行为。
In this case, it is a logical optimization for [[NSString alloc] init]
to return a constant/immortal, rather than creating an empty string for each request. Of course, you should not rely on this detail/behavior in your program.
这篇关于NSString保留计数-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!