Friday, May 31, 2013

NSArray indexOfObjectWithOptions example in Objective C (iOS).


NSArray indexOfObjectWithOptions

Returns the index of an object in the array that passes a test in a given Block for a given set of enumeration options.

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

Parameters of [NSArray indexOfObjectWithOptions]
opts
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).
predicate
The block to apply to elements in the array.
The block takes three arguments:
obj
The element in the array.
idx
The index of the element in the array.
stop
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 indexOfObjectWithOptions]
The index whose corresponding value in the array passes the test specified by predicate and opts. If the opts bit mask specifies reverse order, then the last item that matches is returned. Otherwise, the index of the first matching object is returned. If no objects in the array pass the test, returns NSNotFound.

Discussion of [NSArray indexOfObjectWithOptions]
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 indexOfObjectWithOptions example.
NSArray *arrayOfStrings = ...;
NSString *stringToSearchWithin = ...";
NSUInteger index = [arrayOfStrings indexOfObjectWithOptions:NSEnumerationConcurrent
                                                passingTest:^(id obj, NSUInteger idx, BOOL *stop)
                    {
                        NSString *s = (NSString *)obj;
                        if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) {
                            *stop = YES;
                            return YES;
                        }
                        return NO;
                    }];
if (arrayOfStrings == nil || index == NSNotFound)
{
    NSLog(@"The string does not contain any of the strings from the arrayOfStrings");
    return;
}
NSLog(@"The string contains '%@' from the arrayOfStrings", [arrayOfStrings objectAtIndex:index]);

Example of [NSArray indexOfObjectWithOptions].
NSArray *arrayOfStrings = ...;
NSString *stringToSearchWithin = ...";
__block NSString *result = nil;
[arrayOfStrings indexOfObjectWithOptions:NSEnumerationConcurrent
                             passingTest:^(NSString *obj, NSUInteger idx, BOOL *stop)
    {
        if ([stringToSearchWithin rangeOfString:obj].location != NSNotFound)
        {
            result = obj;
            *stop = YES;
            //return YES;
        }
        return NO;
    }];
if (!result)
    NSLog(@"The string does not contain any of the strings from the arrayOfStrings");
else
    NSLog(@"The string contains '%@' from the arrayOfStrings", result);

NSArray indexOfObjectWithOptions example.
NSUInteger theIndex=NSNotFound;
theIndex=[theArray indexOfObjectWithOptions:NSEnumerationConcurrent passingTest:^(id obj, NSUInteger, idx, BOOL *stop){
     if ([obj rangeOfString: aString]!=NSNotFound]){
         *stop=YES;
         return YES;
     }
     return NO;
}];

End of NSArray indexOfObjectWithOptions example article.