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 unsigned intを意味します)ですが、@1
実際の保持数は次のようになります。異なる時間に7、8、および10。それにルールはありますか?私もできることがわかりました。それ[n1 retain]
に[n1 release]
応じて、保持数が増減します。