Amazon.com Widgets Question on NSIndexSet
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 10:18:06 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 15 - Numbers, Strings, and Collections
| | | |-+  Question on NSIndexSet
Pages: [1]   Go Down
Print
Author Topic: Question on NSIndexSet  (Read 558 times)
bpran
Newbie
*
Posts: 18






« on: October 22, 2011, 02:42:20 AM »

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.
Code: (Objective-C)
//
//  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

Code: (Objective-C)
//
//  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


Code: (Objective-C)
//
//  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

Code: (Objective-C)
//
//  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

Code: (Objective-C)
//
//  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:
« Last Edit: October 22, 2011, 02:47:16 AM by bpran » Logged
bpran
Newbie
*
Posts: 18






« Reply #1 on: October 22, 2011, 05:53:10 AM »

Made changes into the code,
change the AddressBook.h and AddressBook.m

adding the:
Code: (Objective-C)
@property (copy, nonatomic) NSString *bookName;
@property (copy, nonatomic) NSMutableArray *book;

and on the implementation:
Code: (Objective-C)
@synthesize bookName, book;

and then change the main:
Code: (Objective-C)
        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 book] objectAtIndex: idx] print];
             }];
        }
        else
            NSLog(@"Name Not Found!");

I no longer use my 'lookupUsingIndex' method.

Is this the right way?
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.