NSArray / NSMutableArray ã®ã¡ã¢ãªç®¡ç
é åã®ã¡ã¢ãªã¯ã¡ããã¨ç®¡çããã¦ããç¥ãããã£ããã§ç¢ºèªãã¦ããµã³ãã«ä½ã£ã¦ç¢ºããã¦ã¿ã¾ãã.
åãã£ããã¨ã¯ä»¥ä¸ã®ã«ã¼ã«ã§ã.
- åæå : initXXXXã®ã¡ã½ããã§æå®ããããªãã¸ã§ã¯ãã¯å ¨ã¦retainããã. arrayXXXã¯autorelease.
- 追å : 追å ããããªãã¸ã§ã¯ããretainããã.
- åé¤ : 追å ããããªãã¸ã§ã¯ããreleaseããã.
- é åèªä½ãdealloc : å å ãã¦ãããªãã¸ã§ã¯ãå ¨ã¦ãreleaseããã.
æ¤è¨¼ã³ã¼ã
NSObject *objA = [[NSObject alloc] init]; NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:objA, nil]; NSLog(@"after init array; array retainCount:%d expect 1", [array retainCount]); NSLog(@"after init array; objA retainCount:%d expect 2", [objA retainCount]); NSObject *objB = [[NSObject alloc] init]; NSLog(@"after init objB; objB retainCount:%d expect 1", [objB retainCount]); [array addObject:objB]; NSLog(@"after add objB; objB retainCount:%d expect 2", [objB retainCount]); [array removeObject:objB]; NSLog(@"after remove objB; objB retainCount:%d expect 1", [objB retainCount]); NSObject *objC = [[NSObject alloc] init]; [array addObject:objC]; NSLog(@"after add objC; objC retainCount:%d expect 2", [objC retainCount]); [array release]; NSLog(@"after release array; objA retainCount:%d expect 1", [objA retainCount]); NSLog(@"after release array; objC retainCount:%d expect 1", [objC retainCount]); NSObject *objD = [[NSObject alloc] init]; NSArray *array2 = [[NSArray alloc] initWithObjects:objD, nil]; NSLog(@"after init array2; objD retainCount:%d expect 2", [objD retainCount]); [array2 release]; NSLog(@"after release array2; objD retainCount:%d expect 1", [objD retainCount]);
çµæ
after init array; array retainCount:1 expect 1 after init array; objA retainCount:2 expect 2 after init objB; objB retainCount:1 expect 1 after add objB; objB retainCount:2 expect 2 after remove objB; objB retainCount:1 expect 1 after add objC; objC retainCount:2 expect 2 after release array; objA retainCount:1 expect 1 after release array; objC retainCount:1 expect 1 after init array2; objD retainCount:2 expect 2 after release array2; objD retainCount:1 expect 1
çµæãäºæ³éãã®åãã§ãã.