[英]How to understand Retain count
I used following link for understand the retain count. 我使用以下链接来了解保留计数。
retain count in iphone 在iphone中保留计数
As per that question output is : 根据该问题,输出是:
NSString *str = [[NSString alloc] initWithFormat:@"a,b,c,d"];
NSArray *anArray =[[NSArray alloc]init];
NSLog(@"Retain count: %i", [anArray retainCount]);
anArray=[str componentsSeparatedByString:@","];
NSLog(@"Retain count: %i", [anArray retainCount]);
OUTPUT OUTPUT
Retain count: 2
Retain count: 1
When I tried this code in my example : 当我在我的示例中尝试此代码时:
NSString *str = [[NSString alloc] initWithFormat:@"a,b,c,d"];
NSArray *anArray=[[NSArray alloc]init];
NSLog(@"Retain count: %i", [anArray retainCount]);
anArray=[str componentsSeparatedByString:@","];
NSLog(@"Retain count: %i", [anArray retainCount]);
Then OUTPUT is 那么OUTPUT就是
Retain count: 51
Retain count: 1
I can't understand why the retain count of NSArray
is 51 and after assigning value in array it becomes 1. 我无法理解为什么NSArray
的保留计数为51,并且在数组中赋值后它变为1。
I also read 我也读过
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html
http://ranga-iphone-developer.blogspot.in/2011/11/what-is-retain-count-or-reference.html http://ranga-iphone-developer.blogspot.in/2011/11/what-is-retain-count-or-reference.html
iOS about retainCount 关于retainCount的iOS
and other tutorial. 和其他教程。 but I can't understand the value of Retain count: 51
. 但我无法理解Retain count: 51
的价值Retain count: 51
。
Pleas Help me. 请帮助我。
Thank You. 谢谢。
See WhenToUseRetainCount.com for an explanation on when and why to use retainCount
. 见WhenToUseRetainCount.com有关何时以及为什么使用说明retainCount
。
The count is 51 due to an implementation detail of empty arrays. 由于空数组的实现细节,计数为51。 It goes to 1 because you are getting the count of a different object. 它变为1,因为您获得了不同对象的计数。
If using manual-retain-release, then this pattern is a leak: 如果使用manual-retain-release,则此模式是泄漏:
Foo *foo = [[Foo alloc] init];
foo = [someObject retrieveTheFooness];
The allocated object is leaked. 分配的对象被泄露。
Use ARC. 使用ARC。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.