ヒレガス本Chapter4

メモリ管理。よくわからん。iPhoneの場合ガベージコレクションが使えないと思うので、retail,release,autoreleaseは押さえておいた方がよさげ。
ここらは木下本と詳解本にも書いてあるのでそっちも参考になるかも。

以下は、自信無いけどChapter3〜Chapter4を通して作ったプログラム。つまりこの2章はセットなので全部読むか、全部とばすかのどっちかにしたほうがいいとおもう。
コマンドラインでのObjective-Cの書き方がある程度わかっていてGUIアプリの作り方を知りたい人は全部とばしてChapter5に進んでもいいと思う。

LotteryEntry.h

#import <Foundation/Foundation.h>

@interface LotteryEntry : NSObject {
	NSCalendarDate *entryDate;
	int firstNumber;
	int secondNumber;
}

-(void)setEntryDate:(NSCalendarDate *)date;
-(NSCalendarDate*)entryDate;
-(int)firstNumber;
-(int)secondNumber;
-(id)initWithEntryDate:(NSCalendarDate *)theDate;
@end

LotteryEntry.m

#import "LotteryEntry.h"

@implementation LotteryEntry

-(void)setEntryDate:(NSCalendarDate*)date 
{
	[date retain];
	[entryDate release];
	entryDate=date;
}
-(NSCalendarDate*)entryDate
{
	return entryDate;
}
-(int)firstNumber
{
	return firstNumber;
}
-(int)secondNumber
{
	return secondNumber;
}
-(void)dealloc
{
	NSLog(@"deallocing %@", self);
	[entryDate release];
	[super dealloc];
}

-(id)init
{
	return [self initWithEntryDate:[NSCalendarDate calendarDate]];
}

-(id)initWithEntryDate:(NSCalendarDate *)theDate
{
	if(![super init])
		return nil;
	
	entryDate = [theDate retain];
	firstNumber=random()%100+1;
	secondNumber=random()%100+1;
	return self;
}

-(NSString*)description 
{
	NSString *result;
	result=[NSString stringWithFormat:@"%@=%d and %d",
			[self entryDate],
			[self firstNumber], [self secondNumber]];
	return result;
}
@end

lottery.m

#import <Foundation/Foundation.h>
#import "LotteryEntry.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	NSCalendarDate *now=[[NSCalendarDate alloc] init];
	srandom(time(NULL));
    NSMutableArray *array;
	array=[[NSMutableArray alloc] init];
	int i;
	for(i=0;i<10;i++) {
		NSCalendarDate *iWeeksFromNow;
		iWeeksFromNow = [now dateByAddingYears:0 months:0 days:(i*7) hours:0 minutes:0 seconds:0];
		LotteryEntry *newEntry=[[LotteryEntry alloc] initWithEntryDate:iWeeksFromNow];
		[array addObject:newEntry];
		[newEntry release];
	}
	[now release];
	now=nil;
	for(LotteryEntry *entryToPrint in array) {
		NSLog(@"%@", entryToPrint);
	}
	[array release];
	array=nil;
    [pool drain];
	NSLog(@"GC = %@", [NSGarbageCollector defaultCollector]);
    return 0;
}

実行結果

2008-10-02 22:20:44.494 lottery[853:10b] 2008-10-02 22:20:44 +0900=59 and 45
2008-10-02 22:20:44.495 lottery[853:10b] 2008-10-09 22:20:44 +0900=10 and 72
2008-10-02 22:20:44.495 lottery[853:10b] 2008-10-16 22:20:44 +0900=17 and 24
2008-10-02 22:20:44.495 lottery[853:10b] 2008-10-23 22:20:44 +0900=53 and 55
2008-10-02 22:20:44.496 lottery[853:10b] 2008-10-30 22:20:44 +0900=76 and 5
2008-10-02 22:20:44.497 lottery[853:10b] 2008-11-06 22:20:44 +0900=17 and 32
2008-10-02 22:20:44.497 lottery[853:10b] 2008-11-13 22:20:44 +0900=59 and 43
2008-10-02 22:20:44.497 lottery[853:10b] 2008-11-20 22:20:44 +0900=47 and 51
2008-10-02 22:20:44.498 lottery[853:10b] 2008-11-27 22:20:44 +0900=93 and 86
2008-10-02 22:20:44.498 lottery[853:10b] 2008-12-04 22:20:44 +0900=25 and 75
2008-10-02 22:20:44.498 lottery[853:10b] deallocing 2008-10-02 22:20:44 +0900=59 and 45
2008-10-02 22:20:44.499 lottery[853:10b] deallocing 2008-10-09 22:20:44 +0900=10 and 72
2008-10-02 22:20:44.499 lottery[853:10b] deallocing 2008-10-16 22:20:44 +0900=17 and 24
2008-10-02 22:20:44.499 lottery[853:10b] deallocing 2008-10-23 22:20:44 +0900=53 and 55
2008-10-02 22:20:44.500 lottery[853:10b] deallocing 2008-10-30 22:20:44 +0900=76 and 5
2008-10-02 22:20:44.500 lottery[853:10b] deallocing 2008-11-06 22:20:44 +0900=17 and 32
2008-10-02 22:20:44.501 lottery[853:10b] deallocing 2008-11-13 22:20:44 +0900=59 and 43
2008-10-02 22:20:44.501 lottery[853:10b] deallocing 2008-11-20 22:20:44 +0900=47 and 51
2008-10-02 22:20:44.501 lottery[853:10b] deallocing 2008-11-27 22:20:44 +0900=93 and 86
2008-10-02 22:20:44.502 lottery[853:10b] deallocing 2008-12-04 22:20:44 +0900=25 and 75
2008-10-02 22:20:44.508 lottery[853:10b] GC = (null)