Cocoa Programming for Mac OS X - Aaron Hillegass [20]
{
entryDate = date;
}
- (NSDate *)entryDate
{
return entryDate;
}
- (int)firstNumber
{
return firstNumber;
}
- (int)secondNumber
{
return secondNumber;
}
@end
Here is the play-by-play for each method:
prepareRandomNumbers uses the standard random function to generate a pseudorandom number. You use the mod operator (%) and add 1 to get the number in the range 1–100.
setEntryDate: sets the pointer entryDate to a new value.
entryDate, firstNumber, and secondNumber return the values of variables.
Changing main.m
Now let’s look at main.m. Many of the lines have stayed the same, but several have changed. The most important change is that we are using LotteryEntry objects instead of NSNumber objects.
Here is the heavily commented code. (You don’t have to type in the comments.)
#import #import "LotteryEntry.h" int main (int argc, const char *argv[]) { @autoreleasepool { // Create the date object NSDate *now = [[NSDate alloc] init]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *weekComponents = [[NSDateComponents alloc] init]; // Seed the random number generator srandom((unsigned)time(NULL)); NSMutableArray *array; array = [[NSMutableArray alloc] init]; int i; for (i = 0; i < 10; i++) { [weekComponents setWeek:i]; // Create a date/time object that is 'i' weeks from now NSDate *iWeeksFromNow; iWeeksFromNow = [cal dateByAddingComponents:weekComponents toDate:now options:0]; // Create a new instance of LotteryEntry LotteryEntry *newEntry = [[LotteryEntry alloc] init]; [newEntry prepareRandomNumbers]; [newEntry setEntryDate:iWeeksFromNow]; // Add the LotteryEntry object to the array [array addObject:newEntry]; } for (LotteryEntry *entryToPrint in array) { // Display its contents NSLog(@"%@", entryToPrint); } } return 0; } Note the second loop. Here you are using Objective-C’s mechanism for enumerating over the members of a collection. This program will create an array of LotteryEntry objects, as shown in Figure 3.7. Figure 3.7. Object Diagram Implementing a description Method Figure 3.8. Completed Execution Hmm. Not quite what we hoped for. After all, the program is supposed to reveal the dates and the numbers you should play on those dates, and you can’t see either. (You are seeing the default description method as defined in NSObject.) Next, you will make the LotteryEntry objects display themselves in a more meaningful manner. Add a description method to LotteryEntry.m: - (NSString *)description { NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setTimeStyle:NSDateFormatterNoStyle]; [df setDateStyle:NSDateFormatterMediumStyle]; NSString *result; result = [[NSString alloc] initWithFormat:@"%@ = %d and %d", [df stringFromDate:entryDate], firstNumber, secondNumber]; return result; } Build and run the application. Now you should see the dates and numbers: Figure 3.9. Execution with Description NSDate Before moving on to any new ideas, let’s examine NSDate in some depth. Instances of NSDate represent a single point in time and are basically immutable: You can’t change the day or time once it is created. Because NSDate is immutable, many objects often share a single date object. There is seldom any need to create a copy of an NSDate object. Here are some of the commonly used methods implemented by NSDate: + (id)date Creates and returns a date initialized to the current date and time. This is a class method. In the interface file, implementation file, and documentation, class methods are recognizable because they start with + instead of –. A class method is triggered by sending a message to the class instead of an instance. This one, for example, could be used as follows: NSDate *now; now = [NSDate date]; - (id)dateByAddingTimeInterval:(NSTimeInterval)interval Creates and returns a date initialized to the date
Build and run your application. You should see something like Figure 3.8.