Friday, May 31, 2013

NSArray enumerateObjectsAtIndexes options usingBlock example in Objective C (iOS).


NSArray enumerateObjectsAtIndexes options usingBlock


Executes a given block using the objects in the array at the specified indexes.

- (void)enumerateObjectsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

Parameters of [NSArray enumerateObjectsAtIndexes options usingBlock]
indexSet
The indexes of the objects over which to enumerate.
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).
block
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.

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

Important: If the Block parameter or the indexSet is nil this method will raise an exception.

NSArray enumerateObjectsAtIndexes options usingBlock example.
[myArray enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, idx)]    
options:nil usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    }];

Example of [NSArray enumerateObjectsAtIndexes options usingBlock].
NSString *string =@"be";

NSRange range = NSMakeRange(0, 24);

NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange: range];

[array enumerateObjectsAtIndexes:indexSet options: NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger index, BOOL *stop)

{
    //if([obj localizedCaseInsensitiveCompare:string] == NSOrderedSame)
    if([obj hasPrefix:string])

    {
        NSLog(@"Object Found: %@ at index: %i",obj, index);

        *stop=YES;

    }

} ];

NSArray enumerateObjectsAtIndexes options usingBlock example.
            if ([textInput isFirstResponder])
                [textInput.superview.subviews enumerateObjectsAtIndexes:
                 [NSIndexSet indexSetWithIndexesInRange:
                  NSMakeRange([textInput.superview.subviews indexOfObject:textInput]+1,
                              [textInput.superview.subviews count]-[textInput.superview.subviews indexOfObject:textInput]-1)]
                                                                options:0 usingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
                                                                    *stop = !obj.hidden && [obj becomeFirstResponder];
                                                                }];
            if ([textInput isFirstResponder])
                [textInput.superview.subviews enumerateObjectsAtIndexes:
                 [NSIndexSet indexSetWithIndexesInRange:
                  NSMakeRange(0,
                              [textInput.superview.subviews indexOfObject:textInput])]
                                                                options:0 usingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
                                                                    *stop = !obj.hidden && [obj becomeFirstResponder];
                                                                }];

End of NSArray enumerateObjectsAtIndexes options usingBlock example article.