ios [__NSArrayM objectAtIndex:] 崩溃:索引 0 超出空数组错误的界限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7859361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:00:27  来源:igfitidea点击:

Crashes with [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array error

objective-cioscrashnsarray

提问by msetten

I am getting crash reports via users of my iOS app, but crash I can't reproduce myself, nor can I trace the error back to a line in my own code (except that it originates from line 14 in main.m but this is the default application creation of iOS apps. Below the a crash report I received.

我通过我的 iOS 应用程序的用户收到崩溃报告,但崩溃我无法重现,我也无法将错误追溯到我自己代码中的一行(除了它源自 main.m 中的第 14 行,但这是iOS 应用程序的默认应用程序创建。下面是我收到的崩溃报告。

In understand that at some point a object is at index 0 is being retrieved from an empty Array. But since it doesn't seem to point to my own code, can it be a bug in iOS? (It happens on different platforms and with different iOS versions).

据了解,在某个时候,正在从空数组中检索索引为 0 的对象。但是既然好像没有指向我自己的代码,那会不会是iOS的bug?(它发生在不同的平台和不同的 iOS 版本上)。

I hope someone has an idea what is happening or can point me in the right direction. Thanks.

我希望有人知道发生了什么,或者可以为我指明正确的方向。谢谢。

CRASH REPORT:

崩溃报告:

Incident Identifier: [TODO]
CrashReporter Key:   [TODO]
Process:         Mary Black [797]
Path:            /var/mobile/Applications/28A68F8B-294E-4B86-9E75-ED5484E5EF4D/Mary Black.app/Mary Black
Identifier:      net.broset.Mary-Black
Version:         225
Code Type:       ARM (Native)
Parent Process:  launchd [1]

Date/Time:       2011-10-14 03:47:32 +0000
OS Version:      iPhone OS 5.0 (9A334)
Report Version:  104

Exception Type:  SIGTRAP
Exception Codes: #0 at 0x35b07848
Crashed Thread:  0

Application Specific Information:
*** Terminating app due to uncaught exception \\'NSRangeException\\', reason: \\'*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array\\'

Thread 0 Crashed:
0   libsystem_kernel.dylib              0x00010848 __kill + 8
1   CoreFoundation                      0x000b9987 __handleUncaughtException + 75
2   libobjc.A.dylib                     0x000092d1 _objc_terminate + 129
3   libc++abi.dylib                     0x000043c5 _ZL19safe_handler_callerPFvvE + 77
4   libc++abi.dylib                     0x00004451 operator delete(void*) + 1
5   libc++abi.dylib                     0x00005825 __cxa_current_exception_type + 1
6   libobjc.A.dylib                     0x00009235 objc_exception_rethrow + 13
7   CoreFoundation                      0x0000f545 CFRunLoopRunSpecific + 405
8   CoreFoundation                      0x0000f3a5 CFRunLoopRunInMode + 105
9   GraphicsServices                    0x00003fed GSEventRunModal + 157
10  UIKit                               0x00031743 UIApplicationMain + 1091
11  Mary Black                          0x00002fa7 main (main.m:14)

回答by maz

One way to debug this issue is to add a Symbolic Breakpoint on objectAtIndex: This may result in manyhits to objectAtIndex: before the debugger hits the one you're after but it's guaranteed to find it.

调试此问题的一种方法是在 objectAtIndex 上添加一个符号断点:这可能会导致对 objectAtIndex 的多次点击:在调试器点击您要查找的那个之前,但它可以保证找到它。

Steps in Xcode 4:

Xcode 4 中的步骤:

  • View > Navigators > Breakpoint Navigator
  • click '+' and choose Add Symbolic Breakpoint in the pop-up
  • type in "objectAtIndex:" in the Symbol text field and click Done
  • 视图 > 导航器 > 断点导航器
  • 单击“+”并在弹出窗口中选择“添加符号断点”
  • 在符号文本字段中输入“objectAtIndex:”,然后单击完成

at runtime, if you can't tell which objectAtIndex: you're hitting, move the slider at the bottom of the Debug Navigator all the way to the right.

在运行时,如果您无法分辨您正在点击哪个 objectAtIndex:,请将 Debug Navigator 底部的滑块一直向右移动。

回答by Michael Superczynski

This will stop the debugger at the offending line:

这将在违规行停止调试器:

Xcode/Edit Scheme/Diagnostics/Log Exceptions.

Xcode/编辑方案/诊断/日志异常。

回答by vikingosegundo

This error means, that you try to get an object from a NSArray from a position where no object is found. In other words: if the index is 0, than your array is empty.
And most likely it is your code, as main() invokes your code.

此错误意味着,您尝试从未找到对象的位置从 NSArray 获取对象。换句话说:如果索引为 0,则您的数组为空。
并且很可能是您的代码,因为 main() 调用您的代码。

回答by alexantd

You need to search your project for all usages of objectAtIndex:and rule out each one as the culprit. Make sure you are nevercalling it on an empty array.

您需要在您的项目中搜索所有用法objectAtIndex:并排除每一种都是罪魁祸首。确保您永远不会在空数组上调用它。

Another tip: If you know you only want to get the last object in an array, oryou know the array will only ever contain one object, you can use lastObjectinstead of objectAtIndex:-- it's safer because it won't throw an exception, though you still need to check for nil.

另一个提示:如果您知道只想获取数组中的最后一个对象,或者您知道该数组将只包含一个对象,则可以使用lastObject代替objectAtIndex:-- 它更安全,因为它不会抛出异常,尽管您仍然需要检查nil.