它们用于在开发期间检查不变条件.我认为在生产/发布版本中会忽略它们(如this answer中所述).他们不是.相反,它们会在TestFlight测试期间导致崩溃.当我评论断言时,应用程序不会崩溃.有些东西通常会出错,但不会崩溃.
可以用我的构建设置吗?
我的所有归档方案都使用发布配置:
断言位于Cocoa Touch Framework项目中,该项目用于自定义键盘扩展.
所有项目中的所有目标(Cocoa Touch Framework以及具有键盘扩展目标的主项目)都具有以下构建设置:
Enable Foundation Assertions Debug YES Release NO Disable Safety Checks NO
怎么了?
编辑:
Sulthan’s answer显示了如何为调试和重建版本全局禁用断言.这不是我需要的.我希望它按预期工作 – 断言应在调试中启用,但在发布版本中禁用.
默认情况下它以这种方式工作 – 它也在我的主项目中以这种方式工作.但它不适用于从该主项目链接的Framework项目中的断言(详见this question).为什么?怎么解决?
解决方法
Enable Foundation Assertions在预处理部分(宏)中. Swift没有预处理,也没有使用宏.此选项禁用NSAssert,NSParameterAssert和Objective-C中常用的类似宏.
禁用安全检查是一种性能选项:
By default,the standard library guarantees memory safety. Many functions and methods document the requirements that must be satisfied by the caller,such as an array index being valid; memory safety is guaranteed even if a requirement is violated. However,violating a requirement can trigger a runtime error. APIs that include the word “unsafe” in their name let you explicitly disable safety checks in places where you need the additional performance. It’s your responsibility to verify the memory safety of code that uses unsafe APIs. Memory safety is also not guaranteed if there is a race condition in multithreaded code.
你应该尝试我的答案here
(在其他Swift标志中使用-assert-config发布).
或者只是将断言保留在生产版本中.每个失败的断言都是一个错误,一般来说,最好尽快了解一个错误.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。