问题描述
当我使用 NSArray
和 NSString
对象记录保留计数时,我的行为不均匀。
请参阅下面的代码,
When I am logging retain count with NSArray
and NSString
objects, I am having uneven behavior.See the code below,
NSArray *aryTemp = [NSArray arrayWithObjects:@"One",nil];
NSLog(@"Retain Count :%d",[aryTemp retainCount]);
NSArray *aryTemp2 = [[NSArray alloc] initWithObjects:@"One",nil];
NSLog(@"Retain Count :%d",[aryTemp2 retainCount]);
NSString *strTemp = @"One";
NSLog(@"Retain Count :%d",[strTemp retainCount]);
NSString *strTemp2 = [[NSString alloc] initWithString:@"One"];
NSLog(@"Retain Count :%d",[strTemp2 retainCount]);
这是我得到的输出
2011-03-01 19:19:32.410 Test[14607:207] Retain Count :37
2011-03-01 19:19:32.412 Test[14607:207] Retain Count :1
2011-03-01 19:19:32.413 Test[14607:207] Retain Count :2147483647
2011-03-01 19:19:32.413 Test[14607:207] Retain Count :2147483647
代码有什么问题??
谢谢
Pratik Goswami
Pratik Goswami
推荐答案
你永远不应该使用 -retainCount
,因为它永远不会告诉你任何有用的东西。 Foundation和AppKit / UIKit框架的实现是不透明的;你不知道保留什么,为什么保留它,谁保留它,保留它等等。
You should never use -retainCount
, because it never tells you anything useful. The implementation of the Foundation and AppKit/UIKit frameworks is opaque; you don't know what's being retained, why it's being retained, who's retaining it, when it was retained, and so on.
另见:来重新计算你不使用retainCount的原因。
Also see: StackOverflow | when to use retainCount for an excellent recount of why you don't use retainCount.
并回复Dave DeLong:
请大家前往并请求 -retainCount
被弃用。要求的人越多越好。
And to echo Dave DeLong:Please everyone go to http://bugreport.apple.com and request that -retainCount
be deprecated. The more people that ask for it, the better.
这篇关于使用retainCount的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!