Gbacchus's code is on the right track -- nice and clean. I have modified rangeOfString (in AddressBook.m) to solve the case sensitive problem. Thanks to Stephen for pointing out the rangeOfString options. I think "NSCaseInsensitiveSearch" does the trick. Everything seems to be working now.
Question.. I would like to split the "mySearch" method (in AddressBook.m) into two methods: one to search and one to print. I can't seem to get my head around passing the new array ("found") back to the main program, then, calling another method to print it. Any help would be appreciated.
Note that I added a console prompt (scanf) in the main program. This way, the search string is dynamic rather than hard-coded. Great for quickly validating the code.
AddressCard.h
#import <Foundation/Foundation.h>
@interface AddressCard: NSObject
{
NSString *name;
NSString *email;
}
@property (copy, nonatomic) NSString *name, *email;
// Method to set name and email at same time
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
@end
AddressCard.m
#import "AddressCard.h"
@implementation AddressCard
@synthesize name, email;
// Method to set both name and email at once
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
self.name = theName;
self.email = theEmail;
}
@end
AddressBook.h
#import "AddressCard.h"
#import <Foundation/NSArray.h>
@interface AddressBook : NSObject {
NSString * bookName;
NSMutableArray * book;
}
-(id) initWithName: (NSString *) theName;
-(void) addCard: (AddressCard *) theCard;
-(void) list;
-(void) dealloc;
-(AddressCard *) mySearch: (NSString *) theName;
@end
AddressBook.m
#import "AddressBook.h"
@implementation AddressBook
//--------------------------------------------
-(id) initWithName: (NSString *) theName
{
self = [super init];
if ( self)
{
bookName =[ [ NSString alloc] initWithString: theName];
book = [ [ NSMutableArray alloc] init];
}
return self;
}
//--------------------------------------
-(void) addCard: (AddressCard *) theCard
{
[book addObject: theCard];
}
// ------------------------------------
-(AddressCard *) mySearch: (NSString *) theName;
{
NSMutableArray *found;
NSRange substr1, substr2;
found = [[NSMutableArray alloc] init];
for (AddressCard *theCard in book){
substr1 = [theCard.name rangeOfString: theName options:NSCaseInsensitiveSearch];
substr2 = [theCard.email rangeOfString: theName options:NSCaseInsensitiveSearch];
if ((substr1.location != NSNotFound) || (substr2.location != NSNotFound))
[found addObject: theCard];
}
//Print "found" array to console
NSLog(@" ");
NSLog(@"******* Search Results ********");
NSLog(@"Search for: %@", theName);
for (AddressCard * theCard in found)
{
NSLog(@" Name:%-20s %-32s", [theCard.name UTF8String], [theCard.email UTF8String] );
}
if ([found count] == 0)
NSLog(@"Sorry, no records found in search.");
NSLog(@"*****************************");
return nil;
}
//------------------------------------------------
-(void) list
{
NSLog(@"******* AddressBook: %@ ********", bookName);
for (AddressCard * theCard in book)
{
NSLog(@" Name:%-20s %-32s", [theCard.name UTF8String], [theCard.email UTF8String] );
}
}
//-----------------------------------------------------
-(void) dealloc
{
[bookName release];
[book release];
[super dealloc];
}
@end
Main.m
#import "AddressBook.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Create AddressBook class pointer
AddressBook * book = [ [ AddressBook alloc] initWithName: @"Exersise 15-2"];
// Create AddressCard class pointers
AddressCard * card1 = [ [ AddressCard alloc] init];
AddressCard * card2 = [ [ AddressCard alloc] init];
AddressCard * card3 = [ [ AddressCard alloc] init];
AddressCard * card4 = [ [ AddressCard alloc] init];
AddressCard * card5 = [ [ AddressCard alloc] init];
// Set AddressCard variables
[card1 setName: @"George Washington" andEmail: @"george.washington@whitehouse.gov"];
[card2 setName: @"John Adams" andEmail: @"john.adams@whitehouse.gov"];
[card3 setName: @"Thomas Jefferson" andEmail: @"thomas.jefferson@whitehouse.gov"];
[card4 setName: @"James Madison" andEmail: @"james.madison@whitehouse.gov"];
[card5 setName: @"James Monroe" andEmail: @"james.monroe@whitehouse.gov"];
// Add cards (data) to book
[book addCard: card1];
[book addCard: card2];
[book addCard: card3];
[book addCard: card4];
[book addCard: card5];
// Call list method
[book list];
//prompt for user input
char name[100];
NSString *mySearchString;
printf("What name are you looking for? Hint: Enter first few letters.");
scanf("%s", &name);
mySearchString = [NSString stringWithCString: name encoding: NSASCIIStringEncoding];
// Search for string in book
[book mySearch: mySearchString];
//Memory release
[card1 release];
[card2 release];
[card3 release];
[card4 release];
[card5 release];
[book release];
[pool drain];
return 0;
}