Amazon.com Widgets Exercise 15.3 (indexesOfObjectsPassingTest:)
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 01, 2014, 07:21:47 PM
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
| |-+ Answers to Exercises
| | |-+ Chapter 15
| | | |-+ Exercise 15.3 (indexesOfObjectsPassingTest:)
Pages: [1] Go Down
Print
Author Topic: Exercise 15.3 (indexesOfObjectsPassingTest:) (Read 3383 times)
NChiba
Newbie
*
Posts: 6






on: February 13, 2012, 05:46:03 AM

Hi.
Haven't found on forum a solution for Exercise 15.3 for lookupAll method with NSIndexSet class. So here is my solution:

AddressBook.h
Code: (Objective-C)
-(NSArray *) lookupAll: (NSString *) theName;

AddressBook.m
Code: (Objective-C)
-(NSArray *) lookupAll:(NSString *)theName
{
    NSIndexSet *indexSet = [book indexesOfObjectsPassingTest:
                            ^ BOOL(id obj, NSUInteger idx, BOOL *stop) {
                                if ([[obj name] rangeOfString:theName options:NSCaseInsensitiveSearch].location != NSNotFound) {
                                  return YES;
                                } else
                                    return NO;
                            } ];
   
   //    Create array that contains objects at indexes indexSet in array book
    NSArray *result = [[NSArray alloc] initWithArray:[book objectsAtIndexes:indexSet]];

   
    if ([result count]) {
        return result;
    } else
        return nil;
}
main.m
Code: (Objective-C)
NSArray *matches = [[NSArray alloc] initWithArray:[myBook lookupAll:@"steve"]];
                                     
        // Lookup person by name
       
        NSLog(@"Search for Steve");
       
        if (matches != nil) {
            for (AddressCard *elem in matches)
                [elem print];
        } else
            NSLog(@"Nothing found!");
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: February 13, 2012, 10:15:45 AM

Excellent solution!  Thanks for sharing.

Cheers,

Steve
Logged
mitchb
Full Member
***
Posts: 142






Reply #2 on: December 04, 2012, 07:28:20 PM

This is my attempt at this program.

AddressBook.m

Code: (Objective-C)
- (NSIndexSet *)lookupAll:(NSString *)theName
{
   
    NSIndexSet *result = [book indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop)
        {
           
            if ( [[obj name] rangeOfString:theName options:NSCaseInsensitiveSearch].location != NSNotFound )
                return YES;
            else
                return NO;
        }];
    return result;
}
Main.m

Code: (Objective-C)
  NSIndexSet *cardsFound = [NSIndexSet indexSet];
       
        NSLog(@"Lookup: Kochan");
        cardsFound = [myBook lookupAll: @"Kochan"];
       
        if ( [cardsFound count] )
        {
            [cardsFound enumerateIndexesUsingBlock:^ (NSUInteger idx, BOOL *stop)
             {
                 NSLog(@"Match at index: %ld", idx);
                 [myBook.book[idx] print];
             
             }];
        }
        else
            NSLog(@"No matches found!");
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
maarut
Newbie
*
Posts: 1






Reply #3 on: February 01, 2013, 05:32:04 AM

I believe the logic in -(NSArray *) lookupAll:(NSString *)theName can be simplified a little, as per below.

AddressBook.h
Code: (Objective-C)
-(NSArray *) lookupAll: (NSString *) theName;  

AddressBook.m
Code: (Objective-C)
-(NSArray *) lookupAll:(NSString *)theName
{
    return [book objectsAtIndexes:
        [book indexesOfObjectsPassingTest: ^BOOL(id obj, NSUInteger idx, BOOL *stop)
            {
                return [[obj name] rangeOfString:theName options:NSCaseInsensitiveSearch].location != NSNotFound;
            }
}

main.m
Code: (Objective-C)
NSArray *matches = [myBook lookupAll:@"steve"];
                                     
// Lookup person by name
       
NSLog(@"Search for Steve");
       
if ([matches count]) { for (AddressCard *elem in matches) { [elem print]; } }
else { NSLog(@"Nothing found!"); }
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.