Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan

Old Stuff => Chapter 15 => : TotalLuck February 15, 2009, 03:56:28 PM



: Program 15.11
: TotalLuck February 15, 2009, 03:56:28 PM
Lookup method added to AddressBook.h
: (Objective-C)
-(AddressCard *) lookup: (NSString *) theName;
[/pre]

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

return nil;
}
[/pre]

Test Program
: (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 = @"jewls337@axlc.com";
NSString *bName = @"Tony Iannino";
NSString *bEmail = @"tony.iannino@techfitness.com";
NSString *cName = @"Stephen Kochan";
NSString *cEmail = @"steve@kochan-wood.com";
NSString *dName = @"Jamie Baker";
NSString *dEmail = @"jbaker@kochan-wood.com";


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];
else
NSLog(@"Not Found!");

// try another lookup


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

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


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

[pool drain];

return 0;

}

[/pre]


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.