Swift断言生产应用程序中的行为 [英] Swift Assertions behaviour in production applications

查看:107
本文介绍了Swift断言生产应用程序中的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm reading the Assertions section in the Swift e-book and 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 NSAssert promises never to terminate a production application as a result of an assertion failure. Is it the same case in Swift?

推荐答案

基于Apple在他们的文档,我会说在生产环境中会忽略断言。

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


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

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.

在注意块中:


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

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.

这篇关于Swift断言生产应用程序中的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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