1

Many classes have enumeration functions like this one for NSArray:

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

What is the correct way (design pattern) to implement this in your own class? The code below is how I would do it, but that's maybe not the correct way? For argument's sake _contents is an NSArray, but it could be any suitable data structure. Block enumeration is supposed to be faster than fast enumeration. This snippet doesn't seem anything special that would make it faster though.

 - (void)enumerateInputData:(NSRange) range
                      block:(void(^)(NSData *data, NSUInteger index, BOOL *stop)) enumerationBlock
{
    BOOL stop = NO;
    for (NSUInteger index = range.location; index < NSMaxRange(range); index++)
    {
        //custom processing
        NSData *data = [_contents objectAtIndex:index];

        enumerationBlock(data, index,&stop);
        if (stop) {
            break;
        }
    }
}
bneely
  • 9,083
  • 4
  • 38
  • 46
user965972
  • 2,489
  • 2
  • 23
  • 39
  • «Block enumeration is supposed to be faster than fast enumeration.» It is? Concurrent enumeration could potentially speed up the overall task, but that has nothing to do with Blocks. – jscs Mar 02 '14 at 20:06
  • I read that somewhere on stack overflow. It was mentioned in a WWDC 2010 video, apparently. – user965972 Mar 02 '14 at 20:20
  • "Block enumeration is supposed to be faster than fast enumeration": Not necessarily so. It could, I guess, if your implementation used `dispatch_apply` with concurrent queue (and block was reentrant-safe), but otherwise fast enumeration may actually be faster (at least that was what I experienced when I benchmarked it a few months ago). – Rob Mar 02 '14 at 20:59

0 Answers0