學習Foundation時的一個執行個體,把前面講的東西都用到了,還不錯。
上代碼:
//AddressCard介面聲明標頭檔
AddressCard.h
#import <Foundation/NSObject.h><br />#import <Foundation/NSString.h></p><p>@interface AddressCard: NSObject<br />{<br /> NSString *name;<br /> NSString *email;<br />}</p><p>-(void) setName: (NSString *)theName;<br />-(void) setEmail: (NSString *)theEmail;<br />-(void) setName: (NSString *)theName andEmail: (NSString *)theEmail;</p><p>-(NSString *) name;<br />-(NSString *) email;</p><p>-(void) print;<br />-(NSComparisonResult) compareNames: (id) element;<br />-(void) dealloc;<br />@end<br />
//AddressCard介面實現檔案
AddressCard.m
#import "AddressCard.h"</p><p>@implementation AddressCard<br />-(void) setName: (NSString *) theName<br />{<br /> //release old object before dispatch a new one<br /> //(even when name is nil,you also can send it a message)<br /> [name release];<br /> name = [[NSString alloc] initWithString:theName];<br />}</p><p>-(void) setEmail: (NSString *) theEmail<br />{<br /> //release old object before dispatch a new one<br /> [email release];<br /> email = [[NSString alloc] initWithString:theEmail];<br />}</p><p>-(NSString *) name<br />{<br /> return name;<br />}</p><p>-(NSString *) email<br />{<br /> return email;<br />}</p><p>-(void) setName: (NSString *)theName andEmail: (NSString *)theEmail<br />{<br /> [email release];<br /> [name release];<br /> email = [[NSString alloc] initWithString:theEmail];<br /> name = [[NSString alloc] initWithString:theName];</p><p> name = theName;<br /> email = theEmail;<br />}</p><p>-(void) print<br />{<br /> NSLog(@"=====================================");<br /> NSLog(@"| |");<br /> NSLog(@"| %-31s |",[name UTF8String]);<br /> NSLog(@"| %-31s |",[email UTF8String]);<br /> NSLog(@"| |");<br /> NSLog(@"| |");<br /> NSLog(@"| |");<br /> NSLog(@"| O O |");<br /> NSLog(@"=====================================");<br />}</p><p>//Comprae the two names from the specified address cards<br />-(NSComparisonResult) compareNames: (id) element<br />{<br /> return [name compare:[element name]];<br />}</p><p>//for release the object name and email<br />-(void) dealloc<br />{<br /> [name release];<br /> [email release];<br /> [super dealloc];<br />}<br />@end<br />
//AddressBook介面聲明檔案
AddressBook.h
#import <Foundation/NSArray.h><br />#import "AddressCard.h"</p><p>@interface AddressBook: NSObject<br />{<br /> NSString *bookName;<br /> NSMutableArray *book;<br />}</p><p>-(id) initWithName: (NSString *) name;<br />-(void) addCard: (AddressCard *) theCard;<br />-(int) entries;<br />-(void) list;<br />-(void) sort;<br />-(AddressCard *) lookup: (NSString *) theName;<br />-(void) dealloc;</p><p>@end<br />
//AddressBook介面實現檔案
AddressBook.m
#import "AddressBook.h"</p><p>@implementation AddressBook<br />//set up the AddressBook's name and an empty book<br />-(id) initWithName: (NSString *) name<br />{<br /> self = [super init];</p><p> if(self)<br /> {<br /> bookName = [[NSString alloc] initWithString: name];<br /> book = [[NSMutableArray alloc] init];<br /> }</p><p> //because if this class has a subclass and it use this method to initialise,<br /> //the self will not be a AddressBook object,<br /> //so we return a (id)object other than a AddressBook object<br /> return self;</p><p>}</p><p>-(void) addCard:(AddressCard *) theCard<br />{<br /> //add card to the mutable array<br /> [book addObject: theCard];<br />}</p><p>-(int) entries<br />{<br /> //count the num of address card<br /> return [book count];<br />}</p><p>//lookup address card by name - assumes and exact match<br />-(AddressCard *) lookup: (NSString *) theName<br />{<br /> for(int i = 0; i < [book count]; i++)<br /> {<br /> if([[[book objectAtIndex: i] name] caseInsensitiveCompare: theName] == NSOrderedSame)<br /> {<br /> return [book objectAtIndex: i];<br /> }<br /> }<br /> return nil;<br />}</p><p>-(void) list<br />{<br /> NSLog(@"======== Contents of: %@ =========", bookName);<br /> //quickly enum<br /> //for(AddressCard *theCard in book)<br /> //NSLog(@"%-20s %-32s", [theCard.name UTF8String],[theCard.email UTF8String]);<br /> for(int i = 0; i < [book count]; ++i)<br /> {<br /> NSLog(@"%-20s %-32s", [[[book objectAtIndex: i] name] UTF8String],[[[book objectAtIndex: i] email] UTF8String]);<br /> }</p><p> NSLog(@"=============================================");<br />}</p><p>-(void) sort<br />{<br /> [book sortUsingSelector: @selector(compareNames:)];<br />}</p><p>//for deep release<br />-(void) dealloc<br />{<br /> [bookName release];<br /> [book release];<br /> [super dealloc];<br />}<br />@end<br />
//程式主檔案
AddressMain.m
#import "AddressBook.h"<br />#import <Foundation/NSAutoreleasePool.h></p><p>int main(int argc,const char *argv[])<br />{<br /> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];<br /> NSString *aName = @"Julia Kochan";<br /> NSString *aEmail = @"jewls337@axlc.com";<br /> NSString *bName = @"Tony Iannino";<br /> NSString *bEmail = @"tony.iannino@teachfitnees.com";<br /> NSString *cName = @"Stephen Kochan";<br /> NSString *cEmail = @"steve@kochan-wood.com";<br /> NSString *dName = @"Jamie Baker";<br /> NSString *dEmail = @"jbaker@kochan-wood.com";</p><p> AddressCard *card1 = [[AddressCard alloc] init];<br /> AddressCard *card2 = [[AddressCard alloc] init];<br /> AddressCard *card3 = [[AddressCard alloc] init];<br /> AddressCard *card4 = [[AddressCard alloc] init];</p><p> AddressBook *myBook = [AddressBook alloc];<br /> AddressCard *myCard;</p><p> //Firest set up four address cards<br /> [card1 setName: aName andEmail: aEmail];<br /> [card2 setName: bName andEmail: bEmail];<br /> [card3 setName: cName andEmail: cEmail];<br /> [card4 setName: dName andEmail: dEmail];</p><p> //Now initialize the address book<br /> myBook = [myBook initWithName: @"Linda's Address Book"];<br /> NSLog(@"Entries in address book after creation: %i",[myBook entries]);</p><p> //Add some cards to the address book<br /> [myBook addCard: card1];<br /> [myBook addCard: card2];<br /> [myBook addCard: card3];<br /> [myBook addCard: card4];</p><p> NSLog(@"Entries in the address book after adding cards: %i",[myBook entries]);</p><p> //List all the entries in the book now<br /> [myBook list];</p><p> //Sort it and list it again<br /> [myBook sort];<br /> [myBook list];</p><p> //Lookup a person by name<br /> NSLog(@"Look up: Stephen Kochan ");<br /> myCard = [myBook lookup: @"stephen kochan"];</p><p> if(myCard != nil)<br /> [myCard print];<br /> else<br /> NSLog(@"Not found! ");</p><p> [card1 release];<br /> [card2 release];<br /> [card3 release];<br /> [card4 release];<br /> [myBook release];<br /> [pool drain];<br /> return 0;<br />}<br />