Reputation: 37464
Trying to do the following:
NSMutableArray *tmpArr = [_tweets subarrayWithRange:NSMakeRange(0, 10)];
_tweets = [[NSMutableArray alloc] init]; // added this in trial and error debugging
_tweets = tmpArr;
_tweets
is an NSMutableArray
and I'm trying to grab the first 10 objects from it.
However, I receive the following error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 157 beyond bounds [0 .. 9]'
Any ideas?
Upvotes: 0
Views: 594
Reputation: 8158
Add this right before you try grabbing the subrange:
NSLog("%ul", _tweets.length);
NSMutableArray *tmpArr = [_tweets subarrayWithRange:NSMakeRange(0, 10)];
Then, at least you'll know that the array contains the expected number of elements.
You might also want to enable guard malloc in Xcode as it seems you might be running into a memory/pointer issue. Xcode - scribble, guard edges and guard malloc
Upvotes: 0
Reputation: 62052
The error is occurring at some point after this code.
You start with _tweets
, an NSMutableArray as you say, with presumably 157+ elements in it.
You grab the first 10 elements out of the _tweets
array and assign it to tmpArr
, another NSMutableArray.
Now... you take that original _tweets
array, re-alloc and re-init it (which is redundant given the following line. And set _tweets
to point to the same memory location tmpArr
points to.
Now you have two array pointers, _tweets
and tmpArr
. They both point to the exact same memory location, and at that memory location sits an NSMutableArray with 10 elements--the first 10 elements that were originally in _tweets
.
At some point after this code is executed, you're trying to access the 157th element of one of these two array pointers (which again, point to the same array). But the highest index is 9, so the exception is caused. The line of code throwing the exception is not posted in the question.
Upvotes: 2