简体   繁体   中英

Continuous numbers in Objective-C array like range() in Python

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?

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.

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 .

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM