枚举NSArray从数组搜索的中心/中间开始

时间:2013-01-16 09:05:04

标签: ios objective-c nsarray enumeration

如何枚举从数组中心开始的数组?

1 个答案:

答案 0 :(得分:-1)

@implementation NSArray (Extensions)

- (void)enumerateFromCenterGoBothWaysUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
{
    NSMutableArray *copy = [self mutableCopy];
    BOOL shouldStop = NO;
    while([copy count] > 0 && shouldStop == NO)
    {
        NSUInteger index = [copy count] / 2;
        id obj = copy[index];
        [copy removeObject:obj];

        block(obj, index, &shouldStop);
    }
}

@end