Interface and implementation of a simple category on NSArray, named Filter, with a single method that filters numbers.
It is good practice to add a prefix (PF
) to the method to ensure we don't overwrite any future NSArray
methods.
@interface NSArray (PFFilter)
- (NSArray *)pf_filterSmaller:(double)number;
@end
@implementation NSArray (PFFilter)
- (NSArray *)pf_filterSmaller:(double)number
{
NSMutableArray *result = [NSMutableArray array];
for (id val in self)
{
if ([val isKindOfClass:[NSNumber class] && [val doubleValue] >= number)
{
[result addObject:val];
}
}
return [result copy];
}
@end