如何在像obj-c的Map方法的ruby中迭代时跳过对象

abb*_*ood 3 iphone cocoa objective-c ios objective-c-blocks

使用这里的答案,这个方法在obj-c中实现类似于ruby的映射:

- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [result addObject:block(obj, idx)];
    }];
    return result;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果在应用块时发生错误,我怎么能跳过一个对象?通常要跳过枚举器中的某些内容,只需使用该return命令,但这不是上述方法中的一个选项,因为该块应该返回一些内容.

在这个例子中我用return跳过但得到一个错误:

NSArray *mappedArray = [objArray mapObjectsUsingBlock:^(id obj, NSUInteger i) {
    // i don't want this obj to be included in final array
    // so I try to skip it
    return;   // ERROR:incompatible block pointer types sending 
              // 'void(^)(__strong id, NSUInteger)' to parameter of type 
              // 'id(^)(__strong id, NSUInteger)'


    // else do some processing
    return soupedUpObj;
}];
Run Code Online (Sandbox Code Playgroud)

我目前的解决方法是简单地返回一个null对象,然后将它们从最终数组中删除.但我相信一定有比这更好的方法.

Jef*_*eff 5

如果实现与上面显示的类似,那么将块结果应用于中间值然后在将其添加到结果数组之前进行检查是有意义的.像这样的东西:

- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id blockResult = block( obj, idx );
        if( result != nil ){
          [result addObject:blockResult];
        }
    }];
    return result;
}
Run Code Online (Sandbox Code Playgroud)