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?
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.
thank you in advance.
Greg
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?
Code:
-(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;
}
Code:
-(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..
++count;
}
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.
Code:
-(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;
}
else
{
NSLog(@"No entries found!");
return NO;
}
}
thank you in advance.
Greg