A quick listing in the NSArray category for NSIntegers

Since I often use NSInteger arrays, I wrote a category for NSArray (and one for NSMutableArray, too) that adds methods like integerAtIndex :, arrayByAddingInteger: etc. Methods take care of wrapping / deploying an NSInteger in an NSNumber object.

I am wondering if there is a way to improve my category so that I can quickly list NSIntegers. I would like to be able to write:

NSArray* arrayOfIntegers; . . . for(NSInteger nextInteger in arrayOfIntegers) { } 

.... so that "nextInteger" is pulled out of the NSNumber object backstage. Can I do it?

+7
source share
3 answers

I doubt that there is a clean way with NSFastEnumeration, since it is highly dependent on the nextObject method.

But you can do it differently by adding a block method for it:

 @interface NSArray (Integers) -(void)eachInteger:(void(^)(NSInteger))block; @end @implementation NSArray (Integers) -(void)eachInteger:(void(^)(NSInteger))block { for (NSNumber *num in self) { block(num.integerValue); } } @end 

So you can use it in your code in a similar way:

 NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:23], [NSNumber numberWithInt:42], nil]; ... [arr eachInteger:^(NSInteger i) { NSLog(@"The int is %i", i); }]; // => // The int is 23 // The int is 42 

You might want to take a look at the NSArray categories on the Lumumba Framework , which I think is written by me: D

+4
source

This cannot be done, but you can easily convert NSNumber to NSInteger and use it later. You can even write a macro for it:

 #define int_enum(var, arr, block) \ for(NSNumber *__tmp in arr) { NSInteger var = [__tmp integerValue]; block } 

Use it as:

 NSArray *array = // whatever; int_enum(counter, array, { // things you want to do with `counter' as an NSInteger }); 
+3
source

If you really like blocks, try this:

 @interface NSArray(blockIteration) @property (copy, nonatomic, readonly) void (^forEachObject)(void (^block)(NSArray *, id)); @property (copy, nonatomic, readonly) void (^forEachInt)(void (^block)(NSArray *, int)); @property (copy, nonatomic, readonly) void (^forEachDouble)(void (^block)(NSArray *, double)); @end @implementation NSArray(blockIteration) -(void (^)(void (^)(NSArray *, id))) forEachObject { return [^(void (^block)(NSArray *, id)) { block = [block copy]; for (id obj in self) { block(self, obj); } } copy]; } -(void (^)(void (^)(NSArray *, int))) forEachInt { return [^(void (^block)(NSArray *, int)) { block = [block copy]; for (NSNumber *num in self) { block(self, [num intValue]); } } copy]; } -(void (^)(void (^)(NSArray *, double))) forEachDouble { return [^(void (^block)(NSArray *, double)) { block = [block copy]; for (NSNumber *num in self) { block(self, [num doubleValue]); } } copy]; } @end int main() { NSArray *array = [NSArray arrayWithObjects:@"Hello", @"World", @"This", @"Is", @"A", @"Test", nil]; array.forEachObject(^(id arr, id obj) { NSLog(@"%@", obj); }); } 

Note that this implementation is ARC dependent.

0
source

All Articles