简体   繁体   English

Objective-C数组中的连续数字,如Python中的range()

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

Python can make a list with continuous numbers like this: Python可以创建一个包含连续数字的列表,如下所示:

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

How to implement this in Objective-c? 如何在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. 阅读你的陈述“只需要一个带有连续数字的数组,我不想用循环初始化它”让我问:对你来说更重要的是:拥有一个array或拥有代表连续范围的“ 东西 ” (自然)数字。 Have a look at NSIndexSet It may come close to what you want. 看看NSIndexSet它可能接近你想要的。 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. 迭代这个集就像迭代数组一样简单,不需要NSNumbers。

Objective-C (or Foundation actually) does not have a special function for this. Objective-C(或实际基础)对此没有特殊功能。 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

NSRange numbers = NSMakeRange(1, 10);

NSRange is simply a struct and not like a Python range object. NSRange只是一个结构而不是 Python 范围对象。

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

So you have to use for loop to access its members. 因此,您必须使用for循环来访问其成员。

NSUInteger num;
for(num = 1; num <= maxValue; num++ ){
    // Do Something here
}

You can subclass NSArray with a class for ranges. 您可以使用范围类对NSArray进行子类化。 Subclassing NSArray is quite simple: 子类化NSArray非常简单:

  • you need a suitable initialization method, which calls [super init] ; 你需要一个合适的初始化方法,它调用[super init] ; and

  • you need to override count and objectAtIndex: 你需要覆盖countobjectAtIndex:

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. 如果copy RangeArray ,它将成为NSNumber对象的正常数组,但如果您希望通过实现NSCopying协议方法,则可以避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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