NSArray:为什么发送 SIGABRT 而不是“索引越界"类型的错误? [英] NSArray: Why is SIGABRT sent instead of an 'index out of bounds' kind of error?

查看:53
本文介绍了NSArray:为什么发送 SIGABRT 而不是“索引越界"类型的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.所以我在我正在处理的一个复杂的 Objective-C iOS 程序上遇到了这个非常奇怪的 SIGABRT 错误,经过一天的跟踪我找到了罪魁祸首.

Ok. So I had this extremely weird SIGABRT error on a complex Objective-C iOS program that I'm working on, and after one day of tracking I found the culprit.

假设我们有以下代码:

NSArray *a = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSLog(@"tada: %@", [a objectAtIndex:-1]);

为什么这会用 Program received signal: SIGABRT 和调试器甚至不指向我的代码(而是在某些汇编部分)而不是更好的索引越界"来终止程序'和'嘿,这里的这行代码是错误的'错误?

Why the hell will this terminate the program with Program received signal: SIGABRT and the debugger not even pointing to my code (but rather in some assembly part) instead of a nicer 'index out of bounds' and 'hey, this line of code here be wrong' error?

我以为我搞砸了项目配置,所以我在一个全新的项目中复制了这个:相同的结果.

I thought I messed up the project config, so I reproduced this on a brand new project: same result.

有没有办法将 XCode 配置得更好,并以更易于理解的方式指示此类错误?

Is there a way to configure XCode to be more nice and indicate this kind of errors in a more human understandable way ?

推荐答案

如文档所述:

如果索引超出数组的末尾(即如果索引大于大于或等于 count 返回的值),一个 NSRangeException 是提出

If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count), an NSRangeException is raised

当未定义异常处理程序时,默认操作是……好吧……您可以看到默认行为是什么.

And the default action, when no exception handler is defined, is to... well... you can see what the default behaviour is.

可以使用@try/@catch 来捕获异常,但这并不是真正的Objective-C-ish.你知道数组中有多少元素;您实际上没有必要访问不存在的元素.

You can use @try/@catch to trap the exception, but that's not really Objective-C-ish. You know how many elements are in the array; there's no real need for you to be accessing elements that don't exist.

像这样的异常通常有一个堆栈跟踪,所以你可以回到导致错误的代码行.(如果 LLDB 和 GDB 工作不正常,可能值得在 LLDB 和 GDB 之间切换.LLDB 更快、更小,但并不完全可靠.(

Exceptions like this normally have a stack trace, so you can go back to the line of code causing the error. (It might be worth switching between LLDB and GDB if it's not working correctly. LLDB is faster and smaller but not completely reliable.(

这篇关于NSArray:为什么发送 SIGABRT 而不是“索引越界"类型的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆