7

Python can make a list with continuous numbers like this:

numbers=range(1,10); // >> [1,2,3,4,5,6,7,8,9]

How to implement this in Objective-c?

LJNielsenDk
  • 1,414
  • 1
  • 16
  • 32
Mil0R3
  • 3,876
  • 4
  • 33
  • 61
  • what purpose do you need this range for? – holex Aug 03 '12 at 09:33
  • @holex Just need an array with continuous numbers,I do not want to init it with a loop. – Mil0R3 Aug 03 '12 at 09:40
  • then it is very hard to tell what solution is the best for you. the generic solution is to init an array with a loop and you can use that array for everything but there would be some other and more elegant tricks for special cases but we don't know anything about your final goal. – holex Aug 03 '12 at 09:50
  • 1
    possible duplicate of [looping using NSRange](http://stackoverflow.com/questions/8320987/looping-using-nsrange), [Convert a range to NSArray](http://stackoverflow.com/questions/9445565/how-to-convert-a-range-to-nsarray-in-objective-c?lq=1), [Add 1 through a number to NSArray](http://stackoverflow.com/questions/10058007/how-to-add-1-through-a-number-to-a-nsmutablearray?lq=1), [A list or array of sequential integers](http://stackoverflow.com/questions/11693985/a-list-or-array-of-sequential-integers?lq=1) – jscs Aug 03 '12 at 17:17

4 Answers4

11

Reading your statement " Just need an array with continuous numbers,I do not want to init it with a loop" lets me ask: what is more important for you: to have an array or to have "something" that represents a continuous range of (natural) numbers. Have a look at NSIndexSet It may come close to what you want. You initialize it with

[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1,9)]; 

Iterating over this set is as simple as iterating over an array and does not need NSNumbers.

Heinrich Giesen
  • 1,765
  • 1
  • 11
  • 8
6

Objective-C (or Foundation actually) does not have a special function for this. You could use:

NSMutableArray *array = [NSMutableArray array];
for(int i=1; i<10; i++) {
    [array addObject:@(i)]; // @() is the modern objective-c syntax, to box the value into an NSNumber.
}
// If you need an immutable array, add NSArray *immutableArray = [array copy];

If you want to use it more often you could optionally put it in an category.

Patrick Pijnappel
  • 7,317
  • 3
  • 39
  • 39
  • Yes I used this way also,I just want to find a little simple way. – Mil0R3 Aug 03 '12 at 09:44
  • As I put in the edit, if you use it more often you can create a category (link in answer) that would allow you to do: [NSArray arrayWithNumbersFrom:1 to:10]; – Patrick Pijnappel Aug 03 '12 at 09:49
  • @Veelian Why not wrap the above code up in a method and put it in a category on `NSArray`? The signature would be something like `-(NSArray*) arrayWithRangeFrom: (int) start to: (int) stop step: (int) step;` – JeremyP Aug 03 '12 at 09:55
4

You can use NSRange .

NSRange numbers = NSMakeRange(1, 10);

NSRange is simply a struct and not like a Python range object.

typedef struct _NSRange {
       NSUInteger location;
       NSUInteger length;
} NSRange;

So you have to use for loop to access its members.

NSUInteger num;
for(num = 1; num <= maxValue; num++ ){
    // Do Something here
}
Nayan Chauhan
  • 542
  • 5
  • 16
1

You can subclass NSArray with a class for ranges. Subclassing NSArray is quite simple:

  • you need a suitable initialization method, which calls [super init]; and

  • you need to override count and objectAtIndex:

You can do more, but you don't need to. Here is a sketch missing some checking code:

@interface RangeArray : NSArray

- (id) initWithRangeFrom:(NSInteger)firstValue to:(NSInteger)lastValue;

@end

@implementation RangeArray
{
    NSInteger start, count;
}

- (id) initWithRangeFrom:(NSInteger)firstValue to:(NSInteger)lastValue
{
    // should check firstValue < lastValue and take appropriate action if not
    if((self = [super init]))
    {
        start = firstValue;
        count = lastValue - firstValue + 1;
    }
    return self;
}

// to subclass NSArray only need to override count & objectAtIndex:

- (NSUInteger) count
{
    return count;
}

- (id)objectAtIndex:(NSUInteger)index
{
    if (index >= count)
        @throw [NSException exceptionWithName:NSRangeException reason:@"Index out of bounds" userInfo:nil];
    else
        return [NSNumber numberWithInteger:(start + index)];
}

@end

You can use this as follows:

NSArray *myRange = [[RangeArray alloc] initWithRangeFrom:1 to:10];

If you copy a RangeArray it will become a normal array of NSNumber objects, but you can avoid if you wish by implementing the NSCopying protocol methods.

CRD
  • 52,522
  • 5
  • 70
  • 86