9

I am getting the error below, which makes no sense.

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray removeObjectsInRange:]: range {11, 15} extends beyond bounds [0 .. 15]'

What am I doing wrong here? I am within bounds of the array. Does removing the last object in the array cause issues?

CodaFi
  • 43,043
  • 8
  • 107
  • 153
adrian.coroian
  • 582
  • 4
  • 13

1 Answers1

46

The second field of an NSRange is length, not endpoint. You are trying to remove fifteen objects, starting from index 11.

Instead, you want to do something along the lines of:

[myArray removeObjectsInRange:(NSRange){11, 5}];
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
jscs
  • 63,694
  • 13
  • 151
  • 195
  • 1
    @Yar: It's the exact same document, you know. :) – jscs Mar 26 '12 at 21:11
  • I know, but I can never understand when the documents are going to be the same and when iOS and Mac are going to produce different documents (which they do, sometimes, as method differ etc.). They are not in this case, but could they be different from each other? – Dan Rosenstark Mar 26 '12 at 21:20
  • @Yar: That's true, there are weird discrepancies. There's an interesting diagram under the "Foundation classes" section here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html#//apple_ref/doc/uid/TP40002974-CH3-SW20 – jscs Mar 26 '12 at 21:28
  • Ironically that document doesn't change either if you alter the prefix to `ios` ;) Thanks, though, figure 1-7 is interesting. I thought I was missing more stuff on iOS, actually... – Dan Rosenstark Mar 26 '12 at 21:36
  • 1
    You can also use the NSMakeRange(location, length) function to specify ranges. – bw1024 Dec 10 '15 at 07:01