Skip to content

Instantly share code, notes, and snippets.

@dr2050
Created January 18, 2021 02:31
Show Gist options
  • Save dr2050/a2d347d6935b4cd323664f6967d21b36 to your computer and use it in GitHub Desktop.
Save dr2050/a2d347d6935b4cd323664f6967d21b36 to your computer and use it in GitHub Desktop.
Simple block filter on NSArray in Objective-C
#import "NSArray+Filter.h"
@implementation NSArray (Filter)
- (NSArray *) filteredArrayUsingBlock:(BOOL (^)(id obj))block {
NSIndexSet *const filteredIndexes = [self indexesOfObjectsPassingTest:^BOOL (id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
return block(obj);
}];
return [self objectsAtIndexes:filteredIndexes];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment