keepCount 的奇怪行为

发布于 2024-10-20 01:15:42 字数 898 浏览 5 评论 0原文

当我使用 NSArrayNSString 对象记录保留计数时,我的行为不均匀。 请参阅下面的代码,

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

代码有什么问题?

谢普拉蒂克·戈斯瓦米

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]);

And this is the output I am getting

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

What is the wrong with the code??

Thanks

Pratik Goswami

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

堇年纸鸢 2024-10-27 01:15:42

2147483647 没有问题

2147483647 是 INT_MAX。 INT_MAX 是字符串文字(即代码中定义的@“foo”字符串)的保留计数。这意味着他们永远不会被释放。

顺便说一句,不要使用保留计数。

nothing wrong with 2147483647

2147483647 is INT_MAX. And INT_MAX is the retaincount for string literals (ie @"foo" strings defined in your code). This means they will never be released.

btw, don't use retainCount.

稀香 2024-10-27 01:15:42

您永远不应该使用 -retainCount,因为它永远不会告诉您任何有用的信息。 Foundation 和 AppKit/UIKit 框架的实现是不透明的;您不知道保留什么、为什么保留、谁保留、何时保留等等。

另请参阅:StackOverflow |何时使用retainCount 很好地说明了为什么不使用retainCount。

并回应戴夫·德隆:
请大家前往 http://bugreport.apple.com 并请求 -retainCount 已弃用。要求的人越多越好。

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.

Also see: StackOverflow | when to use retainCount for an excellent recount of why you don't use 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.

善良天后 2024-10-27 01:15:42

keepCount 返回 NSUInteger,您应该以这种方式打印它...

NSLog( @"%lu", (unsigned long)[blabla retainCount] );

%d 用于有符号 32 位整数 (int)。 NSUInteger 在 32 位平台上定义为 unsigned int,在 64 位平台上定义为 unsigned long。上面的示例对于 32/64 位平台都是安全的。

retainCount returns NSUInteger and you should print it in this way ...

NSLog( @"%lu", (unsigned long)[blabla retainCount] );

%d is used for signed 32-bit integer (int). NSUInteger is on 32-bit platform defined as unsigned int and on 64-bit platform as unsigned long. Upper example is safe for both 32/64-bit platforms.

穿越时光隧道 2024-10-27 01:15:42

代码有什么问题吗?

您正在调用retainCount。

retainCount 对于调试来说几乎毫无用处,永远不应该在生产代码中使用。对象的保留计数是一个内部实现细节,通常具有难以理解的值。

What is the wrong with the code??

You are calling retainCount.

retainCount is next to useless for debugging and should never, ever, be used in production code. The retain count of an object is an internal implementation detail that will often be of a value that is unfathomable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文