Amazon.com Widgets indexOfObjectPassingTest
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 08:11:31 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
| |-+  Chapter Study
| | |-+  Chapter 15 - Numbers, Strings, and Collections
| | | |-+  indexOfObjectPassingTest
Pages: [1]   Go Down
Print
Author Topic: indexOfObjectPassingTest  (Read 2577 times)
HAC
Newbie
*
Posts: 1






« on: November 13, 2011, 09:48:21 AM »

Hello,
 On pages 364 & 365 of the third edition, it indicates that indexOfObjectPassingTest should call the block once for every element in the array unless the stop variable is set to YES. However, in debug I seen that indexOfObjectPassingTest will stop processing the array once the block returns YES, regardless whether the stop variable has been set to YES or not. In the following example method (from pages 364, 365) the intent is to return an array with all of the address cards which name matches the passed name. However, it will return an array with only one element, even if that are is more than one match.

-(NSMutableArray *) lookupAll: (NSString *) theName
{
    NSMutableArray *matches = [NSMutableArray array];
   
    NSUInteger result = [book indexOfObjectPassingTest:
                          ^ (id obj, NSUInteger idx, BOOL *stop)
                         {   
                              if ([[obj name] caseInsensitiveCompare: theName] ==
                                  NSOrderedSame) {
                                  [matches addObject: obj];
                                  return YES;
                              }
                              else
                                  return NO;
                          }];
   
    // See if we found a match
   
    if ([matches count])
        return matches;
    else
        return nil;
}

So it seems that in the case of indexOfObjectPassingTest the stop variable is intended to stop processing only in the case where no element has passed the criteria. Please correct me if I'm wrong. 

-thanks,
Hugo.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: November 13, 2011, 08:14:27 PM »

The indexOfObjectPassingTest: method will only return the lowest index number that passes the test, no matter how many times you call it.   It seems as though through your experimentation that you discovered that it will stop once it finds a match, which I guess seems logical.  To get all the index numbers, you need to use the indexesOfObject:   Interesting observation!

Cheers,

Steve
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.