Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 10, 2018, 03:24:33 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Program 15.11  (Read 2624 times)
Full Member
Posts: 107

WWW Email
« on: February 15, 2009, 03:56:28 PM »

Lookup method added to AddressBook.h
Code: (Objective-C)
-(AddressCard *) lookup: (NSString *) theName;

Lookup method added to AddressBook.m
Code: (Objective-C)
-(AddressCard *) lookup: (NSString *) theName
for (AddressCard *nextCard in book)
if ( [[nextCard name] caseInsensitiveCompare: theName] == NSOrderedSame)
return nextCard;

return nil;

Test Program
Code: (Objective-C)
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import "AddressBook.h"

int main(int argc, char *argv[])
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *aName = @"Julia Kochan";
NSString *aEmail = @"";
NSString *bName = @"Tony Iannino";
NSString *bEmail = @"";
NSString *cName = @"Stephen Kochan";
NSString *cEmail = @"";
NSString *dName = @"Jamie Baker";
NSString *dEmail = @"";

AddressCard *card1 = [[ AddressCard alloc] init];
AddressCard *card2 = [[ AddressCard alloc] init];
AddressCard *card3 = [[ AddressCard alloc] init];
AddressCard *card4 = [[ AddressCard alloc] init];

// set up Four addressCards

[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];

AddressBook *myBook = [ AddressBook alloc];
AddressCard *myCard;

// Now initialize the Adress Book
myBook = [ myBook initWithName:@"L=inda's Address Book"];

//add some cards to the address book
[myBook addCard: card1];
[myBook addCard: card2];
[myBook addCard: card3];
[myBook addCard: card4];

// look up a person by name

NSLog (@"Lookup: Stephen Kochan");
myCard = [myBook lookup: @"Stephen Kochan"];

if (myCard != nil)
[myCard print];
NSLog(@"Not Found!");

// try another lookup

NSLog (@"Lookup: Haibo Zhang");
myCard = [myBook lookup: @"Haibo Zhang"];

if (myCard != nil)
[myCard print];
NSLog(@"Not Found!");

[card1 release];
[card2 release];
[card3 release];
[card4 release];

[pool drain];

return 0;



Apps available on  iTunes store:
"ADACode"  iPhone and iPad versions, "ADAGuidelines" iPhone & iPad versions,  "Rehabilitation Act of 1973" for iPhone
"APokerTimer" now for iPhone
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.