我创建了的Xcode 4.4.1空iOS应用,并做了以下内容:
NSNumber *n1 = @1;
NSNumber *n2 = @2;
NSNumber *n3 = @3;
NSNumber *n100 = @100;
NSString *s = @"haha";
NSArray *a = @[n1, s];
NSDictionary *d = @{ @"ha" : @1, @3 : @"hello" };
NSLog(@"retain count of @1 is %i", [n1 retainCount]);
NSLog(@"retain count of @2 is %i", [n2 retainCount]);
NSLog(@"retain count of @3 is %i", [n3 retainCount]);
NSLog(@"retain count of @100 is %i", [n100 retainCount]);
NSLog(@"retain count of @\"haha\" is %i", [s retainCount]);
NSLog(@"retain count of array literal is %i", [a retainCount]);
NSLog(@"retain count of dictionary literal is %i", [d retainCount]);
其结果是:
retain count of @1 is 10
retain count of @2 is 4
retain count of @3 is 5
retain count of @100 is 1
retain count of @"haha" is -1
retain count of array literal is 1
retain count of dictionary literal is 1
所以保留阵列的数量文字和字典文字是1,和字符串是说存在对整个应用程序的运行,所以这就是为什么它是-1(大概意思MAX无符号整数),但保留计数的@1
真正到来出为7,图8和10在不同的时间。 是否有它的规则? 我发现我可以做[n1 retain]
和[n1 release]
还有,它会增加,相应减少了保留计数。