0

I'm brand new to Objective-C, I've got a decent understanding though of Ruby.

I want to have a list/array of numbers going from 1, 2, 3, ... all the way to x (x being a maximum defined elsewhere in my code).

What is the best way to do it (x can be a high number in the millions, so entering each integer manually would be undesirable). The numbers are in normal sequence.

In Ruby, I'd write it something like this:

y = [1..x]
cyehia
  • 79
  • 1
  • 6
  • 1
    possible duplicate of [Convert a range to NSArray](http://stackoverflow.com/questions/9445565/how-to-convert-a-range-to-nsarray-in-objective-c/) or [looping using NSRange](http://stackoverflow.com/questions/8320987/looping-using-nsrange) or [Add 1 through some other number to an NSMutableArray](http://stackoverflow.com/questions/10058007/how-to-add-1-through-a-number-to-a-nsmutablearray?lq=1); also [Most efficient way to generate a series of numbers](http://stackoverflow.com/questions/11238211/what-is-the-most-efficient-way-to-generate-a-sequence-of-nsnumbers?lq=1), – jscs Jul 27 '12 at 18:58

1 Answers1

2

Do you truly need an NSArray, or do you just need an object that represents this range? If it's the latter, you can use an NSIndexSet, as in

NSIndexSet *idxSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, x-1)];

If you do need an NSArray then Josh Caswell's links are probably your best bet.

Lily Ballard
  • 169,315
  • 25
  • 364
  • 333
  • That `NSIndexSet` looks promising, but it seems I can't do a proper enumeration (at least the way I know how) in a "for ... in" block. I'm going to use that `for (int i = 1; i < x; ++i) { [anArray addObject:[NSNumber numberWithInt:i]]; }` that Josh linked. But thank you :) – cyehia Jul 27 '12 at 19:25
  • @cyehia: `for ... in` only works when the value is objects. But `NSIndexSet` supports other enumeration, notably block-based enumeration with `-enumerateIndexesUsingBlock:` and related methods. – Lily Ballard Jul 27 '12 at 19:30
  • @cyehia Could you not just do something like: `for (int x = tIndexSet.firstIndex; x <= tIndexSet.lastIndex; x++)`? – FreeAsInBeer Jul 27 '12 at 20:11
  • @FreeAsInBeer: Not if the index set has gaps. Your increment step needs to say `x = [tIndexSet indexGreaterThanIndex:x]`. And if you use that then the condition step should be `x != NSNotFound` – Lily Ballard Jul 27 '12 at 20:12
  • @KevinBallard Well, I assumed from the way the OP phrased his question that there were no gaps. – FreeAsInBeer Jul 27 '12 at 20:13
  • @KevinBallard This kind of notation implies that the next item in the list will be incremented by one '1, 2, 3, ... all the way to x'. – FreeAsInBeer Jul 27 '12 at 20:14
  • @FreeAsInBeer: Yes, but if you don't write the iteration code correctly, you'll probably get it wrong the next time you have to iterate an `NSIndexSet` where you can't guarantee no gaps. – Lily Ballard Jul 27 '12 at 20:21
  • @KevinBallard Of course, but your statement holds true of a great number of lines of code. "If you write line X incorrectly, it won't work correctly in all situations." I guess I thought that was understood. – FreeAsInBeer Jul 27 '12 at 20:29
  • @FreeAsInBeer: It's pretty easy to write the correct code. No sense trying to take a shortcut here and potentially causing other readers (including your future self) to erroneously believe that's how you iterate over NSIndexSets in general. – Lily Ballard Jul 27 '12 at 20:39
  • @KevinBallard True, but it really depends on his use case. Perhaps he needs to know the integers which are not contained in the set. It really comes down to one's specific needs at a point in time, and, as always, there are a hundred and one ways of doing things. – FreeAsInBeer Jul 27 '12 at 20:46