0

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?

benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • The error is occurring after this bit of code. – nhgrif Apr 09 '14 at 11:29
  • Show the code when you try grab the first 10 object from that array. – Greg Apr 09 '14 at 11:30
  • @nhgrif oh what!? Does the debugger give any form of line number/Class that the error originated from? – benhowdle89 Apr 09 '14 at 11:30
  • @Greg Er, is this it: `[_tweets subarrayWithRange:NSMakeRange(0, 10)];` still an iOS n00b :) – benhowdle89 Apr 09 '14 at 11:30
  • No. But no where in the posted code do you try accessing the 157th element. – nhgrif Apr 09 '14 at 11:30
  • The error isn't in the code you've posted. It is somewhere where you access the objects of tmpArr / _tweets – tilo Apr 09 '14 at 11:32
  • You can search your project for calls to `_tweets objectAtIndex:` and `tmpArr objectAtIndex:`. It's possible that the 157th element access is made by some other call, but this would be an easy thing to Ctrl+F for first. – nhgrif Apr 09 '14 at 11:36
  • Why do you want re- alloc+init the array? Why don't you just `removeAllObjects`? As far as I can tell, it's an address pointer issue. – n00bProgrammer Apr 09 '14 at 11:39
  • Use the force, also known as the debugger. What does it tell you? Specifically if `_tweets` contains enough objects to actually get ten of them? – Monolo Apr 09 '14 at 11:48
  • Why would he even remove all objects from the array @n00bProgrammer ? ARC will deallocate the array assuming there are no other references to it. – nhgrif Apr 09 '14 at 21:30

2 Answers2

2

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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

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

Community
  • 1
  • 1
RobertJoseph
  • 7,968
  • 12
  • 68
  • 113
  • 1
    Why? When the app crashes (which this code wouldn't prevent), it'll tell you the size of the array for which you try grabbing an out of bounds index. See his error message? `*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 157 beyond bounds [0 .. 9]'` – nhgrif Apr 09 '14 at 21:27
  • All I was trying to do was help you ensure he had the correct number of elements in his array. I'll let you help from here on out. – RobertJoseph Apr 10 '14 at 12:48