Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Feb 12, 2009, 10:08 AM   #1
macrumors newbie
Join Date: Jan 2009
Location: Moreno Vallley
Send a message via Yahoo to TotalLuck
Kochan 2.0 book chapter 15 exercise 2-6

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.
Finding my inner Nerd
NEVER EVER make a drunken bet or you could wind up with a picture like mine.
TotalLuck is offline   0 Reply With Quote
Old Feb 12, 2009, 10:40 AM   #2
macrumors 68040
lee1210's Avatar
Join Date: Jan 2005
Location: Dallas, TX
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.

lee1210 is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Learning Obj C - Kochan or Big Nerd Ranch? afousek iPhone/iPad Programming 4 Apr 26, 2014 01:45 PM
Children's board or chapter book creation JackieMB Mac Applications and Mac App Store 0 Aug 28, 2012 07:44 PM

Forum Jump

All times are GMT -5. The time now is 11:57 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC