Hello,
I tried to retrieve object from NSMutableArray at specific index.
The index is related to the 'lookup' search using NSIndexSet on page 365.
I have to come up with new method 'lookupUsingIndex' that I created in AddressBook class.
Hard to explain with words, please help me see my code and see is there faster way to retrieve the objects.
//
// AddressCard.h
//
#import <Foundation/Foundation.h>
@interface AddressCard : NSObject
{
NSString *name;
NSString *email;
NSNumber *age;
}
@property (copy, nonatomic) NSString *name, *email;
@property (copy, nonatomic) NSNumber *age;
- (id) initWithName: (NSString *) theName AndEmail: (NSString *) theEmail andAge: (NSNumber *) theAge;
- (void) print;
- (void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAge: (NSNumber *) theAge;
@end
//
// AddressCard.m
//
#import "AddressCard.h"
@implementation AddressCard
@synthesize name, email, age;
- (id) initWithName: (NSString *) theName AndEmail: (NSString *) theEmail andAge: (NSNumber *) theAge
{
self = [super init];
if (self)
{
[self setName: theName andEmail: theEmail andAge: theAge];
}
return self;
}
- (id) init
{
return [self initWithName: @"NoName" AndEmail: @"NoEmail" andAge: 0];
}
- (void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAge: (NSNumber *) theAge
{
[self setName: theName];
[self setEmail: theEmail];
[self setAge: theAge];
}
- (void) print
{
NSLog (@"===================================");
NSLog (@"| |");
NSLog (@"| %-31s |", [name UTF8String]);
NSLog (@"| %-31s |", [email UTF8String]);
NSLog (@"| Age : %@ |", [self age]);
NSLog (@"| |");
NSLog (@"| |");
NSLog (@"| O O |");
NSLog (@"===================================");
NSLog (@" ");
}
@end
//
// AddressBook.h
//
#import <Foundation/Foundation.h>
#import "AddressCard.h"
@interface AddressBook : NSObject
{
NSString *bookName;
NSMutableArray *book;
}
- (id) initWithName: (NSString *) name;
- (void) addCard: (AddressCard *) theCard;
- (void) removeCard: (AddressCard *) theCard;
- (NSUInteger) entries;
- (void) list;
- (AddressCard *) lookup: (NSString *) theName;
- (AddressCard *) lookupUsingIndex : (NSUInteger) v;
- (NSIndexSet *) lookupAll: (NSString *) theName;
@end
//
// AddressBook.m
//
#import "AddressBook.h"
@implementation AddressBook
-(id) initWithName: (NSString *) name
{
self = [super init];
if (self)
{
bookName = [[NSString alloc] initWithString: name];
book = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addCard: (AddressCard *) theCard
{
[book addObject: theCard];
}
- (void) removeCard: (AddressCard *) theCard
{
[book removeObjectIdenticalTo: theCard];
}
-(NSUInteger) entries
{
return [book count];
}
-(void) list;
{
NSLog (@"======== Contents of: %@ =========", bookName);
for ( AddressCard *theCard in book )
NSLog(@"%-15s %-32s %li", [[theCard name] UTF8String],
[[theCard email] UTF8String],
[[theCard age] integerValue]);
NSLog (@"====================================================");
NSLog(@" ");
}
- (AddressCard *) lookup: (NSString *) theName
{
NSUInteger result = [book indexOfObjectPassingTest: ^ (id obj, NSUInteger idx, BOOL *stop)
{
if ([[obj name] caseInsensitiveCompare: theName] == NSOrderedSame)
{
*stop = YES;
return YES;
}
else
return NO;
}];
if (result != NSNotFound)
return [book objectAtIndex: result];
else
return nil;
}
- (AddressCard *) lookupUsingIndex : (NSUInteger) v
{
NSUInteger result = [book indexOfObjectPassingTest: ^ (id obj, NSUInteger idx, BOOL *stop)
{
if (idx == v)
{
*stop = YES;
return YES;
}
else
return NO;
}];
if (result !=NSNotFound)
return [book objectAtIndex: result];
else
return nil;
}
- (NSIndexSet *) lookupAll: (NSString *) theName
{
NSIndexSet *result = [book indexesOfObjectsPassingTest: ^ (id obj, NSUInteger idx, BOOL *stop)
{
if ([[obj name] caseInsensitiveCompare: theName] == NSOrderedSame)
{
return YES;
}
else
return NO;
}];
return result;
}
- (id)init
{
return [self initWithName: @"NoName"];
}
@end
//
// main.m
//
#import <Foundation/Foundation.h>
#import "AddressBook.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSString *aName = @"Julia Kochan";
NSString *aEmail = @"337jewls@axlc.com";
NSNumber *aAge = [NSNumber numberWithInteger: 25];
NSString *bName = @"Tony Iannino";
NSString *bEmail = @"2tony.iannino@techfitness.com";
NSNumber *bAge = [NSNumber numberWithInteger: 17];
NSString *cName = @"Stephen Kochan";
NSString *cEmail = @"4steve@classroomM.com";
NSNumber *cAge = [NSNumber numberWithInteger: 40];
NSString *dName = @"Jamie Baker";
NSString *dEmail = @"1jbaker@classroomM.com";
NSNumber *dAge = [NSNumber numberWithInteger: 35];
NSString *eName = @"Jamie baker";
NSString *eEmail = @"5bakerJ@yahoo.com";
NSNumber *eAge = [NSNumber numberWithInteger: 18];
NSString *fName = @"jamie baker";
NSString *fEmail = @"8jb@bakerclass.com";
NSNumber *fAge = [NSNumber numberWithInteger: 55];
NSString *gName = @"John Doe";
NSString *gEmail = @"6bbb@gmail.com";
NSNumber *gAge = [NSNumber numberWithInteger: 12];
NSString *hName = @"Barack Obama";
NSString *hEmail = @"7obama@whitehouse.com";
NSNumber *hAge = [NSNumber numberWithInteger: 60];
AddressCard *card1 = [[AddressCard alloc] initWithName: aName AndEmail: aEmail andAge: aAge],
*card2 = [[AddressCard alloc] initWithName: bName AndEmail: bEmail andAge: bAge],
*card3 = [[AddressCard alloc] initWithName: cName AndEmail: cEmail andAge: cAge],
*card4 = [[AddressCard alloc] initWithName: dName AndEmail: dEmail andAge: dAge],
*card5 = [[AddressCard alloc] initWithName: eName AndEmail: eEmail andAge: eAge],
*card6 = [[AddressCard alloc] initWithName: fName AndEmail: fEmail andAge: fAge],
*card7 = [[AddressCard alloc] initWithName: gName AndEmail: gEmail andAge: gAge],
*card8 = [[AddressCard alloc] initWithName: hName AndEmail: hEmail andAge: hAge],
*myCard;
NSIndexSet *mySearchIndexList = [NSIndexSet indexSet];
AddressBook *myBook = [[AddressBook alloc] initWithName: @"Linda's Address Book"];
NSLog(@"Entries in address book after creation: %li", [myBook entries]);
[myBook addCard: card1];
[myBook addCard: card2];
[myBook addCard: card3];
[myBook addCard: card4];
[myBook addCard: card5];
[myBook addCard: card6];
[myBook addCard: card7];
[myBook addCard: card8];
NSLog(@"Entries in address book after adding cards: %li", [myBook entries]);
[myBook list];
NSString *searchName1 = @"Stephen Kochan";
NSLog(@"Searching for name : %@", searchName1);
myCard = [myBook lookup: searchName1];
if (myCard != nil)
{
[myCard print];
}
else
NSLog(@"Name Not Found!");
NSString *searchName2 = @"Jamie Baker";
NSLog(@"Searching for name : %@", searchName2);
mySearchIndexList = [myBook lookupAll: searchName2];
NSLog(@"Total count for mySearchIndexList = %li ",[mySearchIndexList count]);
if ([mySearchIndexList count] != 0)
{
[mySearchIndexList enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop)
{
NSLog(@"index number %li in the book",idx);
[[myBook lookupUsingIndex: idx] print];
}];
}
else
NSLog(@"Name Not Found!");
}
return 0;
}
Output:
