View Full Version : Kochan 2.0 book chapter 15 exercise 2-6

Feb 12, 2009, 10:08 AM
I am working through the 2.0 book and Chapter 15 exercise 2-6 work with an address book. it took me a while to get the result but i did get it. my question is if it can be done in a better way that i am missing?

Ex. 2 asks to modify a lookup to return partial matches to names anywhere within the name.
Ex. 3 continues to modify the lookup to search for all matches, returning an Array or nil
Ex. 4 adds more fields to the address card completing all info (ie. phone first, last name)
Ex. 5 modifies the Lookup to perform a search on all the fields of a card and if there is an easier way to search all the fields without it knowing all the fields.?

i made an array of all the string objects in aCard in order to search them easier. ( for me anyway). was there an easier way to get the lookup to search all the fields of a card another way?

-(NSMutableArray *) condenseCard
NSMutableArray *card = [[NSMutableArray alloc] initWithObjects: [self firstName], [self lastName],
[self streetAddress], [self streetName], [self city], [self state], [self zipCode],
[self email], [self homePhone], [self cellPhone], nil];

return card;

-(NSMutableArray *) lookup: (NSString *) theName
NSRange range; // the range for the string lookup
NSMutableArray *lookupResults, *card; // create the return array and the temporary array for search
NSString *tempString; // the string from the temporary array at each object index

lookupResults = [[NSMutableArray alloc]init];
card = [[NSMutableArray alloc] init];

for (AddressCard *nextCard in book)
int count = 0;
card = [nextCard condenseCard]; // create an array of strings from an address card
for ( int i =0; i < [card count]; ++i) // search each string in the array for the lookup string
tempString = [card objectAtIndex: i];
range = [tempString rangeOfString: theName options:NSCaseInsensitiveSearch ];
if ( range.location != NSNotFound ) // if the string is found , increase the count, repeat..
if (count > 0) // if any strings match the lookup string add the address card to the result-array
[lookupResults addObject: nextCard];

return lookupResults;

in my .M files i get 7 warnings about 'NSString may not respond to (in this instance nextCard at the For loop) it compiles and runs but I'm not sure why it gives those warnings?

Ex. 6 adds a removename method.
-(BOOL) removeName: (NSString *) theName
NSMutableArray *lookupResults;

lookupResults = [self lookup: theName];
if ([lookupResults count] > 1)
NSLog(@"Multiple entries found. NO name removed");
return NO;
else if ([lookupResults count] == 1)
[self removeCard:[lookupResults objectAtIndex: 0 ]];
return YES;
NSLog(@"No entries found!");
return NO;

thank you in advance.

Feb 12, 2009, 10:40 AM
I wasn't totally clear on the warnings you were getting, so i am not sure there. I would suggest that once a match is found, just add the current item to lookupResults, then break out of the for loop. If you've found a match, why bother looking at all of those other things?

The condensing then searching is a little cumbersome, but it will work. If you are using condenseCard elsewhere it makes a little more sense, but otherwise it wouldn't be that difficult to just enumerate the fields in lookup, and check for:

if(!found) {
... check next field ...

This is really just a matter of style.