Using the answer here , this method achieves something similar to a ruby card in obj-c:
- (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; }
My question is: how can I skip an object if something goes wrong while applying the block? Usually , to skip something in the enumerator, just use the return command, however this is not an option in the method above, since the block is expected to return something.
In this example, I use return to skip, but I get an error:
NSArray *mappedArray = [objArray mapObjectsUsingBlock:^(id obj, NSUInteger i) {
My current way of working with it simply returns a null object, and then removes them from the final array. But I'm sure there should be a better way.
ios objective-c iphone cocoa objective-c-blocks
abbood Aug 21 '13 at 14:43 2013-08-21 14:43
source share