xcode 生产应用程序中的 Swift 断言行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24037935/
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-09-15 05:00:03  来源:igfitidea点击:

Swift Assertions behaviour in production applications

iosobjective-cxcodeassertswift

提问by Stavash

I'm reading the Assertions section in the Swift e-bookand it looks like assertions work very similarly to their Objective-C counterparts. However, nowhere in the docs can I find anything about runtime behaviour while running as a production app. Objective-C's NSAssertpromises never to terminate a production application as a result of an assertion failure. Is it the same case in Swift?

我正在阅读Swift 电子书中的断言部分,看起来断言的工作方式与它们的 Objective-C 对应物非常相似。但是,在文档中找不到任何有关作为生产应用程序运行时的运行时行为的信息。Objective-CNSAssert承诺永远不会因为断言失败而终止生产应用程序。在 Swift 中也是这样吗?

采纳答案by FreeAsInBeer

Based upon the language Apple use in their documentation, I'd say assertions are ignored in a production environment.

根据 Apple 在其文档中使用的语言,我想说在生产环境中会忽略断言。

If your code triggers an assertion while running in a debug environment, such as when you build and run an app in Xcode, you can see exactly where the invalid state occurred and query the state of your app at the time that the assertion was triggered. An assertion also lets you provide a suitable debug message as to the nature of the assert.

如果您的代码在调试环境中运行时触发断言,例如在 Xcode 中构建和运行应用程序时,您可以准确查看无效状态发生的位置,并查询触发断言时应用程序的状态。断言还允许您提供关于断言性质的合适的调试消息。

And in the "Note" block:

在“注意”块中:

Assertions cause your app to terminate and are not a substitute for designing your code in such a way that invalid conditions are unlikely to arise. Nonetheless, 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.

断言会导致您的应用程序终止,并且不能替代以不太可能出现无效条件的方式设计您的代码。尽管如此,在可能出现无效条件的情况下,断言是一种有效的方式,可确保在您的应用程序发布之前,在开发过程中突出显示并注意到此类条件

回答by Sulthan

The difference between debug and release is the difference between compiler arguments. The most likely answer is that there will be some special compiler settings (similar to -eain Java).

调试和发布之间的区别在于编译器参数之间的区别。最可能的答案是会有一些特殊的编译器设置(类似于-eaJava)。

EDIT
The Swift compiler has an argument called -assert-config

编辑
Swift 编译器有一个参数叫做-assert-config

-assert-config Specify the assert_configuration replacement. Possible values are Debug, Release, Replacement.

-assert-config 指定 assert_configuration 替换。可能的值为调试、发布、替换。

In Release, the assertions are ignored. Not sure about the difference between Debugand Replacement.

在 中Release,断言被忽略。不确定Debug和之间的区别Replacement

enter image description here

在此处输入图片说明

回答by Joseph Lord

Asserts are documented along with preconditions in the Swift standard library documentation.

断言与先决条件一起记录在 Swift 标准库文档中。

  • Debug -> Stop on assertion failure.
  • Release -> Compile with assertions ignored
  • Release and "Disable safety checks" -> Assume all assertion statements are statements of truth and hints to the compiler so that following and preceding code may be removed if only reached in conditions where the assertion would have failed. This means that if you follow the assertion by code on handle the exceptional case it may be ignored. If the assertion would ever have failed the behaviour is completely undefined.
  • 调试 -> 断言失败时停止。
  • 发布 -> 编译时忽略断言
  • 发布和“禁用安全检查”-> 假设所有断言语句都是对编译器的真实陈述和提示,以便仅在断言失败的情况下才能删除后面和前面的代码。这意味着如果您在处理异常情况时通过代码遵循断言,它可能会被忽略。如果断言失败,则行为是完全未定义的。

I've not checked but the "Disable safety checks" may correlate with the -assert-config Replacement that @Sulthan mentions.

我没有检查过,但“禁用安全检查”可能与 @Sulthan 提到的 -assert-config 替换相关。

回答by onmyway133

Check your Optimization Leveland make sure it is not Ononefor release configuration. See my note https://github.com/onmyway133/blog/issues/39

检查Optimization Level并确保它不是Onone用于发布配置。看我的笔记https://github.com/onmyway133/blog/issues/39