NSMutableArray+Selector.h:
#import <Foundation/Foundation.h> @interface NSMutableArray (Selector) -(NSMutableArray*) oddElements; @end
NSMutableArray+Selector.m
#import "NSMutableArray+Selector.h" @implementation NSMutableArray (Selector) -(NSMutableArray *) oddElements { NSMutableArray *x = [NSMutableArray array]; for(NSNumber *number in self) { if (number.intValue % 2 == 1) { [x addObject: number]; } } return x; } @end
To use the categories:
#include <CoreFoundation/CoreFoundation.h> #include "ThePerson.h" #include "NSMutableArray+Selector.h" void demoArray() { @autoreleasepool { NSMutableArray *items = [NSMutableArray array]; [items addObject: [NSNumber numberWithInteger:42]]; [items addObject: [NSNumber numberWithInteger:11]]; [items addObject: [NSNumber numberWithInteger:5]]; [items addObject: [NSNumber numberWithInteger:1976]]; for(NSNumber *number in [items oddElements]) { printf("\n%d", number.intValue); } } } int main(int argc, const char * argv[]) { demoArray(); return 0; }
No comments:
Post a Comment