Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Exercise 9-6 Kochan 2.0. Error? mdeh Mac Programming 9 Feb 2, 2009 09:10 PM
Exercise 9-4 Kochan 2.0. A question mdeh Mac Programming 10 Jan 21, 2009 04:20 PM
Exercise 8-6 Kochan 2.0 mdeh Mac Programming 1 Jan 18, 2009 02:14 AM
Kochan 2.0 Exercise 8-7 Possible with NSLog? mdeh Mac Programming 3 Jan 17, 2009 09:04 PM
Exercise 8-4 of Kochan 2.0 mdeh Mac Programming 10 Jan 17, 2009 06:23 PM

All times are GMT -5. The time now is 05:03 PM.

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

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC