Skip to content

Instantly share code, notes, and snippets.

@jparise
Created January 4, 2012 00:17
Show Gist options
  • Save jparise/1557727 to your computer and use it in GitHub Desktop.
Save jparise/1557727 to your computer and use it in GitHub Desktop.
NSArray Predicates
#import <Foundation/NSArray.h>
@interface NSArray (Predicates)
- (NSArray *)arrayOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate;
- (NSArray *)arrayOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate;
@end
@implementation NSArray (Predicates)
- (NSArray *)arrayOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
{
NSIndexSet *indexes = [self indexesOfObjectsPassingTest:predicate];
return [self objectsAtIndexes:indexes];
}
- (NSArray *)arrayOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
{
NSIndexSet *indexes = [self indexesOfObjectsWithOptions:opts passingTest:predicate];
return [self objectsAtIndexes:indexes];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment