Objective-C NSMutableArray:removeObjectsInRange

发布于 2025-01-06 09:07:42 字数 2828 浏览 4 评论 0原文

我有一个名为 rawData 的 NSMutableArray,大小为 198792。每个索引都包含一个名为 DataSet 的 NSObject。 DataSet 的接口如下:

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

我尝试使用以下代码行修剪 rawData:

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

其中 StartTime*freq-1 = 11799。

我在运行时收到错误: * 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSMutableArray objectAtIndex:]: 索引 198791 超出范围 [0 .. 186992]”

感谢您的帮助!

编辑:堆栈跟踪

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”.

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”.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2025-01-13 09:07:42

您的数组不能是您想象的大小。

查看错误消息:索引超出范围 [0 .. 186992]。因此,您的数组的大小实际上是 186993。

我还建议您在运行时记录 StartTime*freq-1 的值,并将其与数组的长度进行比较。

另外,这里可能导致问题的是它是一个可变数组,并且您要从中删除内容,因此长度会在运行时发生变化。这就是为什么我建议在代码中断处同时记录长度和范围。

所以,像这样:

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

更新:如何将对象添加到另一个数组而不是从当前数组中删除。

你需要使用这个方法来获取一个包含一定范围内的对象的数组:

- (NSArray *)subarrayWithRange:(NSRange)range

然后你可以将这些对象添加到另一个数组中,例如:

// 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)];

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)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文