-4

I have a NSMutableArray called rawData of size 198792. Each index contains an NSObject called DataSet. The interface for a DataSet is as follows:

@interface DataSet : NSObject {
NSNumber *rightFoot;
NSNumber *leftFoot;
}

I am trying to trim down the rawData using the following line of code:

[rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)];

where StartTime*freq-1 = 11799.

I am getting an error during runtime: * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 198791 beyond bounds [0 .. 186992]'

Thank you for any help!

EDIT: stack trace

2012-02-16 17:59:31.671 fwd_analysis[8154:207] *** Terminating app due to uncaught    
exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index    
198791 beyond bounds [0 .. 186992]'

*** Call stack at first throw:

(

0   CoreFoundation                      0x00dc25a9 __exceptionPreprocess + 185

1   libobjc.A.dylib                     0x00f16313 objc_exception_throw + 44

2   CoreFoundation                      0x00db80a5 -[__NSArrayM objectAtIndex:] + 261

3   fwd_analysis                        0x00002a99 -[fwd_analysisViewController      startButtonPressed:] + 252

4   UIKit                               0x002b24fd -[UIApplication sendAction:to:from:forEvent:] + 119

5   UIKit                               0x00342799 -[UIControl sendAction:to:forEvent:] + 67

6   UIKit                               0x00344c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527

7   UIKit                               0x003437d8 -[UIControl touchesEnded:withEvent:] + 458

8   UIKit                               0x002d6ded -[UIWindow _sendTouchesForEvent:] + 567

9   UIKit                               0x002b7c37 -[UIApplication sendEvent:] + 447

10  UIKit                               0x002bcf2e _UIApplicationHandleEvent + 7576

11  GraphicsServices                    0x0171a992 PurpleEventCallback + 1550

12  CoreFoundation                      0x00da3944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52

13  CoreFoundation                      0x00d03cf7 __CFRunLoopDoSource1 + 215


14  CoreFoundation                      0x00d00f83 __CFRunLoopRun + 979

15  CoreFoundation                      0x00d00840 CFRunLoopRunSpecific + 208

16  CoreFoundation                      0x00d00761 CFRunLoopRunInMode + 97

17  GraphicsServices                    0x017191c4 GSEventRunModal + 217

18  GraphicsServices                    0x01719289 GSEventRun + 115

19  UIKit                               0x002c0c93 UIApplicationMain + 1160

20  fwd_analysis                        0x00002758 main + 102

21  fwd_analysis                        0x000026e9 start + 53

)

terminate called after throwing an instance of 'NSException'

Current language:  auto; currently objective-c

Program received signal:  “SIGABRT”.
exolaris
  • 27
  • 3
  • 9
  • You need to post the stack trace of your exception, and you need to post the code where the exception is occurring. To do that, you need to [create an exception breakpoint](http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4). – rob mayoff Feb 16 '12 at 23:02
  • @robmayoff I posted the stack trace and the code where the exception is occuring is: [rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)]; – exolaris Feb 16 '12 at 23:24

1 Answers1

2

Your array can't be the size you think it is.

Look at the error message: index beyond bounds [0 .. 186992]. Therefore the size of your array is actually 186993.

I would also recommend you log the value of StartTime*freq-1 at runtime, and compare it with the length of your array.

Also, something that could cause issues here is that it's a mutable array, and you're removing stuff from it, so the length will change at runtime. This is why I recommend logging both the length and range at the point where the code breaks.

So, something like this:

NSLog(@"Length: %i",[rawData length]);
NSLog(@"Range: %i",StartTime*freq-1);
[rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)];

Update: how to add objects to another array instead of deleting from the current one.

You need to use this method to get an array which contains objects in a certain range:

- (NSArray *)subarrayWithRange:(NSRange)range

You can then add these objects into another array, for example:

// before you did this:
[rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)];
// to create a filtered array do:
NSArray *filtered = [rawData subarrayWithRange:NSMakeRange(0, StartTime*freq-1)];
Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
  • I have a breakpoint set on the line "[rawData removeObjectsInRange:NSMakeRange(0, StartTime*freq-1)];" and during runtime I hover over rawData and it tells me the length of my array is 198792 and that starttime*freq-1 = 11799. – exolaris Feb 16 '12 at 23:16
  • @user1210342 see updated answer - the main problem here is you're removing objects from an array dynamically. try a different approach - put the trimmed down data into a new array - that way you don't have to worry about ranges – Alex Coplan Feb 16 '12 at 23:19
  • I did as you said and this was the result: 2012-02-16 18:21:57.218 fwd_analysis[8464:207] Length: 198792 2012-02-16 18:21:57.220 fwd_analysis[8464:207] Range: 11799 – exolaris Feb 16 '12 at 23:22
  • And you still get that exception? – Alex Coplan Feb 16 '12 at 23:25
  • I would really recommend using a separate `NSMutableArray` and putting the trimmed results in there, instead of deleting from the current one. Without being able to see all your code I can't really help you with trying to get the ranged deletion working – Alex Coplan Feb 16 '12 at 23:30
  • I'm not sure how to do that because the method removeObjectsInRange does not return anything. Can you tell me how I could do that? – exolaris Feb 16 '12 at 23:35
  • Unfortunately that didn't work either, it's saying pretty much the same thing except now it thinks my 'rawData' array is even smaller: "index 198791 beyond bounds [0 .. 11798]'". Note that 11798 is one less than 'starttime*freq-1'. This must have to do with what you were saying before about removing objects dynamically but I have no idea how to fix this. I am using the function exactly as the documentation tells me to. – exolaris Feb 16 '12 at 23:54