This is a very odd error that I'm getting. It's this:
'NSRangeException', reason: '*** -[NSArray subarrayWithRange:]: range {2, 3} extends beyond bounds [0 .. 3]'
What I don't understand is that the range (2,3) clearly doesn't extent beyond the bounds of range (0,3)
In fact, it is within those bounds because I set up the array and range like so:
NSRange range = NSMakeRange([wordArray indexOfObject:letterInWordArray]+1,wordArray.count-1);
NSMutableArray* sortedSubArray = [[NSMutableArray alloc] initWithArray:[wordArray subarrayWithRange:range]];
Essentially I am iterating through an array called wordArray, and I want to create a subarray that is all of the items I haven't iterated through yet. Can anyone help me out here?
What I don't understand is that the range (2,3) clearly doesn't extent beyond the bounds of range (0,3)
It does: the second element of NSRange
is its length, so (2, 3)
includes indexes 2
, 3
, and 4
.
Therefore, if [wordArray indexOfObject:letterInWordArray]
returns a value above zero, the range that you get back would extend beyond the limits of the corresponding array.
To fix this, assign [wordArray indexOfObject:letterInWordArray]
to a variable first, then subtract that number from the total length. This would give you a range extending from the match position to the end of the array.