Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a range to NSArray in Objective-C

I know how to do it in Ruby, converting a range of numbers to an array. But how is it possible in Objective-C?

Ruby: (1..100).to_a

like image 950
joostevilghost Avatar asked Dec 04 '22 05:12

joostevilghost


2 Answers

You've got to do it manually:

// Assuming you've got a "NSRange range;"
NSMutableArray *array = [NSMutableArray array];
for (NSUInteger i = range.location; i < range.location + range.length; i++) {
    [array addObject:[NSNumber numberWithUnsignedInteger:i]];
}
like image 68
DarkDust Avatar answered Dec 27 '22 15:12

DarkDust


You might want to try NSIndexSet instead.

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 100)];
like image 36
Jarsen Avatar answered Dec 27 '22 15:12

Jarsen