如何正确处理Objective-C中永远不会发生的情况?

时间:2022-09-07 09:38:12

I have a condition that should actually never happen, but if this does happen how does one deal with this in Objective-C?

我有一个实际上永远不会发生的情况,但如果确实发生这种情况,那么在Objective-C中如何解决这个问题呢?

In the java domain I would through a Runtime exception, which stops the program from executing. (I would still get a detailed stack trace and be able to determine the cause when debugging).

在java域中,我会通过运行时异常来阻止程序执行。 (我仍然会得到详细的堆栈跟踪,并能够在调试时确定原因)。

Objective-C does have exceptions, but from what I gather this is not the norm to use, one should rather use NSError in general.

Objective-C确实有例外,但从我收集的内容来看,这不是使用的规范,一般应该使用NSError。

Should one still use NSError for such cases or is there some other magic I can perform such as exit(1)?

如果仍然在这种情况下使用NSError,或者我可以执行其他一些魔术,例如exit(1)?

I need something that is valid and will fly by Apple; What is the best ways to deal with this scenario?

我需要一些有效的东西,它会被苹果公司带走;处理这种情况的最佳方法是什么?

1 个解决方案

#1


1  

First, you should add assertions for many, if not all, of these cases so that they will be caught during development and fixed.

首先,您应该为许多(如果不是全部)这些情况添加断言,以便它们在开发期间被捕获并修复。

In production, the assertions will not be hit so you have two options, let the application crash or try to fail gracefully. Which of the two of these you choose depends on a ton of things. Can you fail gracefully? Does this put you in a really bad state if you can't?

在生产中,断言不会被命中,因此您有两个选项,让应用程序崩溃或尝试优雅地失败。您选择的其中两个取决于大量的东西。你能优雅地失败吗?如果你不能,这会让你处于一个非常糟糕的状态吗?

This question can't be fully answered - it depends on the application, the use-case, the situation/circumstances. Maybe you just return early... Maybe you let it crash... Maybe you return an error and tell the user some useful message to fix it... I can't recommend anything without having a full understanding of your app and the thing that "should never happen".

这个问题不能完全回答 - 这取决于应用程序,用例,情况/情况。也许你只是早点回来......也许你让它崩溃......也许你会回复一个错误并告诉用户一些有用的消息来解决它...我不能推荐任何东西而不完全了解你的应用程序和“永远不应该发生”的事情。

#1


1  

First, you should add assertions for many, if not all, of these cases so that they will be caught during development and fixed.

首先,您应该为许多(如果不是全部)这些情况添加断言,以便它们在开发期间被捕获并修复。

In production, the assertions will not be hit so you have two options, let the application crash or try to fail gracefully. Which of the two of these you choose depends on a ton of things. Can you fail gracefully? Does this put you in a really bad state if you can't?

在生产中,断言不会被命中,因此您有两个选项,让应用程序崩溃或尝试优雅地失败。您选择的其中两个取决于大量的东西。你能优雅地失败吗?如果你不能,这会让你处于一个非常糟糕的状态吗?

This question can't be fully answered - it depends on the application, the use-case, the situation/circumstances. Maybe you just return early... Maybe you let it crash... Maybe you return an error and tell the user some useful message to fix it... I can't recommend anything without having a full understanding of your app and the thing that "should never happen".

这个问题不能完全回答 - 这取决于应用程序,用例,情况/情况。也许你只是早点回来......也许你让它崩溃......也许你会回复一个错误并告诉用户一些有用的消息来解决它...我不能推荐任何东西而不完全了解你的应用程序和“永远不应该发生”的事情。