Ray's playground

 

Objective-C(Chapter 3 of Cocoa Programming for Mac OS X)

 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 @interface LotteryEntry : NSObject {
 5     NSCalendarDate *entryDate;
 6     int firstNumber;
 7     int secondNumber;
 8 }
 9 
10 - (void)prepareRandomNumbers;
11 - (void)setEntryDate:(NSCalendarDate *)date;
12 - (NSCalendarDate *)entryDate;
13 - (int)firstNumber;
14 - (int)secondNumber;
15 
16 @end

 

 1 #import "LotteryEntry.h"
 2 
 3 
 4 @implementation LotteryEntry
 5 
 6 - (void)prepareRandomNumbers
 7 {
 8     firstNumber = random() % 100 + 1;
 9     secondNumber = random() % 100 + 1;
10 }
11 
12 - (void)setEntryDate:(NSCalendarDate *)date
13 {
14     entryDate = date;
15 }
16 
17 - (NSCalendarDate *)entryDate
18 {
19     return entryDate;
20 }
21 
22 - (int)firstNumber
23 {
24     return firstNumber;
25 }
26 
27 - (int)secondNumber
28 {
29     return secondNumber;
30 }
31 
32 - (NSString *)description
33 {
34     NSString *result = [[NSString alloc] initWithFormat:@"%@ = %d and %d", [entryDate descriptionWithCalendarFormat:@"%b %d %Y"], firstNumber, secondNumber];
35     return result;
36 }
37 @end

 

 

 1 #include <Foundation/Foundation.h>
 2 #include "LotteryEntry.h"
 3 
 4 int main (int argc, const char * argv[]) {
 5     // insert code here...
 6     
 7     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 8     
 9     NSCalendarDate *now = [[NSCalendarDate alloc] init];
10     srand(time(NULL));
11     
12     
13     NSMutableArray *array;
14     array = [[NSMutableArray alloc] init];
15     
16     int i;
17     for (i=0; i<10; i++
18     {
19         NSCalendarDate *iWeeksFromNow;
20         iWeeksFromNow = [now dateByAddingYears:0 months:0 days:(i*7) hours:0 minutes:0 seconds:0];
21         LotteryEntry *entry = [[LotteryEntry alloc] init];
22         [entry prepareRandomNumbers];
23         [entry setEntryDate:iWeeksFromNow];
24         
25         [array    addObject:entry];
26     }
27         
28     for(LotteryEntry *entryToPrint in array)
29     {
30         NSLog(@"%@", entryToPrint);
31     }
32     
33     [pool drain];
34     
35     return 0;
36 }

 

 

posted on 2011-01-10 10:45  Ray Z  阅读(274)  评论(0编辑  收藏  举报

导航