Friday, May 31, 2013

NSArray indexesOfObjectsWithOptions passingTest example in Objective C (iOS).

NSArray indexesOfObjectsWithOptions passingTest

Returns the indexes of objects in the array that pass a test in a given Block for a given set of enumeration options.

- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

Parameters of [NSArray indexesOfObjectsWithOptions passingTest]
A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).
The block to apply to elements in the array.
The block takes three arguments:
The element in the array.
The index of the element in the array.
A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.
The Block returns a Boolean value that indicates whether obj passed the test.

Return Value of [NSArray indexesOfObjectsWithOptions passingTest]
The indexes whose corresponding values in the array pass the test specified by predicate. If no objects in the array pass the test, returns an empty index set.

Discussion of [NSArray indexesOfObjectsWithOptions passingTest]
By default, the enumeration starts with the first object and continues serially through the array to the last object. You can specify NSEnumerationConcurrent and/or NSEnumerationReverse as enumeration options to modify this behavior.

NSArray indexesOfObjectsWithOptions passingTest example.
    NSArray *array = @[@24, @32, @126, @1, @98, @16, @67, @42, @44];
    // run test block on each element of the array, starting at the end of the array
    NSIndexSet *hits = [array indexesOfObjectsWithOptions:NSEnumerationReverse passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        // if we're past the elements we're interested in
        // we can set the `stop` pointer to YES to break out of
        // the enumeration
        if (idx < [array count] - 5) {
            *stop = YES;
            return NO;
        // do our test -- if the element matches, return YES
        if (40 > [obj intValue]) {
            return YES;
        return NO;
    // indexes of matching elements are in `hits`
    NSLog(@"%@", hits);

Example of [NSArray indexesOfObjectsWithOptions passingTest].
NSIndexSet *indexSet=[allImageURLs indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    BOOL match=NO;
    NSRange twoXRange=[((NSURL *)obj).absoluteString rangeOfString:@"@2x"];
    NSRange iPhoneRange=[((NSURL *)obj).absoluteString rangeOfString:@"~ipad"];
    if (twoXRange.location==NSNotFound && iPhoneRange.location==NSNotFound) {
    return  match;

self.imageURLs=[allImageURLs objectsAtIndexes: indexSet];

NSArray indexesOfObjectsWithOptions passingTest example.
NSIndexSet *theSet=[coursesinfo indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    BOOL match=NO;

    if( obj.finalGrade<100 data-blogger-escaped-br="" data-blogger-escaped-nbsp="">        match=YES;
    return match;

NSArray *courses=[coursesinfo objectsAtIndexes: theSet];

End of NSArray indexesOfObjectsWithOptions passingTest example article.