Objective-C block enumeration design pattern
-
19-10-2022 - |
Question
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;
}
}
}
Pas de solution correcte
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow