Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [21]

By Root 919 0
represented by the receiver plus the given interval.

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate

Returns the interval in seconds between the receiver and anotherDate. If the receiver is earlier than anotherDate, the return value is negative. NSTimeInterval is the same as double.

+ (NSTimeInterval)timeIntervalSinceReferenceDate

Returns the interval in seconds between the first instant of January 1, 2001 GMT and the receiver’s time.

- (NSComparisonResult)compare:(NSDate *)otherDate

Returns NSOrderedAscending if the receiver is earlier than otherDate, NSOrderedDescending if otherDate is earlier, or NSOrderedSame if the receiver and otherDate are equal.

Writing Initializers


Notice the following lines in your main function:

newEntry = [[LotteryEntry alloc] init];

[newEntry prepareRandomNumbers];

You are creating a new instance and then immediately calling prepareRandomNumbers to initialize firstNumber and secondNumber. This is something that should be handled by the initializer, so you are going to override the init method in your LotteryEntry class.

In the LotteryEntry.m file, change the method prepareRandomNumbers into an init method:

- (id)init

{

self = [super init];

if (self)

{

firstNumber = ((int)random() % 100) + 1;

secondNumber = ((int)random() % 100) + 1;

}

return self;

}

The init method calls the superclass’s initializer at the beginning, initializes its own variables, and then returns self, a pointer to the object itself (the object that is running this method). (If you are a Java or C++ programmer, self is the same as the this pointer.)

Now delete the following line in main.m:

[newEntry prepareRandomNumbers];

In LotteryEntry.h, delete the following declaration:

- (void)prepareRandomNumbers;

Build and run your program to reassure yourself that it still works.

Take another look at our init method. Why do we bother to assign the return value of the superclass’s initializer to self and then test the value of self? The answer is that the initializers of some Cocoa classes will return nil if initialization was impossible. In order to handle these cases gracefully, we must both test the return value of [super init] and return the appropriate value for self from our initiailizer.

This pattern is debated among some Objective-C programmers. Some say that it is unnecessary, since most classes’ initializers don’t fail, and most classes’ initializers don’t return a different value for self. We believe it best to be in the habit of assigning to self and testing that value. The effort required is minimal compared to the debugging headaches that await you if you make an incorrect assumption about the superclass’s behavior.

Initializers with Arguments


Look at the same place in main.m. It should now look like this:

LotteryEntry *newEntry = [[LotteryEntry alloc] init]; [newEntry setEntryDate:iWeeksFromNow];

It might be nicer if you could supply the date as an argument to the initializer. Change those lines to look like this:

LotteryEntry *newEntry = [[LotteryEntry alloc]

initWithEntryDate:iWeeksFromNow];

You may see a compiler error; ignore it, as we are about to fix the problem.

Next, declare the method in LotteryEntry.h:

- (id)initWithEntryDate:(NSDate *)theDate;

Now, change (and rename) the init method in LotteryEntry.m:

- (id)initWithEntryDate:(NSDate *)theDate

{

self = [super init];

if (self)

{

entryDate = theDate;

firstNumber = ((int)random() % 100) + 1;

secondNumber = ((int)random() % 100) + 1;

}

return self;

}

Build and run your program. It should work correctly.

However, your class LotteryEntry has a problem. You are going to e-mail the class to your friend Rex. Rex plans to use the class LotteryEntry in his program but might not realize that you have written initWithEntryDate:. If he made this mistake, he might write the following lines of code:

NSDate *today = [NSDate date];

LotteryEntry *bigWin = [[LotteryEntry alloc] init];

[bigWin setEntryDate:today];

Return Main Page Previous Page Next Page

®Online Book Reader