有什么方法可以在Swift中捕获断言吗?

时间:2022-10-13 13:04:52

It seems that Swift doesn't have C#/Java-like exceptions and uses assertions instead. However, the book says that in production environment, they instantly crash the app. Isn't there a way around it? What about unit tests, how can I test that a certain function asserts that it gets a correct input value?

似乎Swift没有类似C#/ Java的异常,而是使用断言。但是,该书说,在生产环境中,它们会立即使应用程序崩溃。是不是有办法绕过它?单元测试怎么样,我如何测试某个函数断言它获得了正确的输入值?

3 个解决方案

#1


1  

From Apple books, The Swift Programming Language it's seems erros should be handle using enum.

从Apple书籍,Swift编程语言来看,使用枚举似乎应该处理错误。

Here is an example from the book.

这是本书的一个例子。

enum ServerResponse {
    case Result(String, String)
    case Error(String)
}

let success = ServerResponse.Result("6:00 am", "8:09 pm")
let failure = ServerResponse.Error("Out of cheese.")

switch success {
case let .Result(sunrise, sunset):
    let serverResponse = "Sunrise is at \(sunrise) and sunset is at \(sunset)."
case let .Error(error):
    let serverResponse = "Failure...  \(error)"
}

From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/br/jEUH0.l

来自:Apple Inc.“The Swift Programming Language。”iBooks。 https://itun.es/br/jEUH0.l

For unexpected erros you should use NSException as point out by lfalin

对于意外的错误,你应该使用NSException作为lfalin的指出

#2


2  

As you mentioned, assertions will crash your app in debug or production. They aren't designed to function like Java/C# exceptions. Their only real purpose is:

正如您所提到的,断言会使您的应用程序在调试或生产中崩溃。它们的设计不像Java / C#异常。他们唯一真正的目的是:

Use an assertion whenever a condition has the potential to be false, but must definitely be true in order for your code to continue execution. ... in situations where invalid conditions are possible, an assertion is an effective way to ensure that such conditions are highlighted and noticed during development, before your app is published.

只要条件可能为false,就使用断言,但必须确保为了使代码继续执行。 ...在可能出现无效条件的情况下,断言是确保在应用程序发布之前在开发过程中突出显示并注意到这些条件的有效方法。

Since you can use Cocoa classes in Swift, you're still able to use NSException for exceptional things that your code can handle.

由于您可以在Swift中使用Cocoa类,因此您仍然可以将NSException用于代码可以处理的特殊事物。

#3


2  

You can add try-catch support for Swift by following the instructions in this article: https://medium.com/@_willfalcon/adding-try-catch-to-swift-71ab27bcb5b8

您可以按照本文中的说明为Swift添加try-catch支持:https://medium.com/@_willfalcon/adding-try-catch-to-swift-71ab27bcb5b8

Basically, it's a wrapper around Obj-C try-catch

基本上,它是Obj-C try-catch的包装器

#1


1  

From Apple books, The Swift Programming Language it's seems erros should be handle using enum.

从Apple书籍,Swift编程语言来看,使用枚举似乎应该处理错误。

Here is an example from the book.

这是本书的一个例子。

enum ServerResponse {
    case Result(String, String)
    case Error(String)
}

let success = ServerResponse.Result("6:00 am", "8:09 pm")
let failure = ServerResponse.Error("Out of cheese.")

switch success {
case let .Result(sunrise, sunset):
    let serverResponse = "Sunrise is at \(sunrise) and sunset is at \(sunset)."
case let .Error(error):
    let serverResponse = "Failure...  \(error)"
}

From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/br/jEUH0.l

来自:Apple Inc.“The Swift Programming Language。”iBooks。 https://itun.es/br/jEUH0.l

For unexpected erros you should use NSException as point out by lfalin

对于意外的错误,你应该使用NSException作为lfalin的指出

#2


2  

As you mentioned, assertions will crash your app in debug or production. They aren't designed to function like Java/C# exceptions. Their only real purpose is:

正如您所提到的,断言会使您的应用程序在调试或生产中崩溃。它们的设计不像Java / C#异常。他们唯一真正的目的是:

Use an assertion whenever a condition has the potential to be false, but must definitely be true in order for your code to continue execution. ... in situations where invalid conditions are possible, an assertion is an effective way to ensure that such conditions are highlighted and noticed during development, before your app is published.

只要条件可能为false,就使用断言,但必须确保为了使代码继续执行。 ...在可能出现无效条件的情况下,断言是确保在应用程序发布之前在开发过程中突出显示并注意到这些条件的有效方法。

Since you can use Cocoa classes in Swift, you're still able to use NSException for exceptional things that your code can handle.

由于您可以在Swift中使用Cocoa类,因此您仍然可以将NSException用于代码可以处理的特殊事物。

#3


2  

You can add try-catch support for Swift by following the instructions in this article: https://medium.com/@_willfalcon/adding-try-catch-to-swift-71ab27bcb5b8

您可以按照本文中的说明为Swift添加try-catch支持:https://medium.com/@_willfalcon/adding-try-catch-to-swift-71ab27bcb5b8

Basically, it's a wrapper around Obj-C try-catch

基本上,它是Obj-C try-catch的包装器