swift中的assert

来源:互联网 发布:该域名非本账户域名 编辑:程序博客网 时间:2024/05/17 23:24

In some cases, it is simply not possible for your code to continue execution if a particular condition is not satisfied. In these situations, you can trigger an assertion in your code to end code execution and to provide an opportunity to debug the cause of the absent or invalid value.
在某些情况下,如果不满足特定条件,您的代码不可能继续执行。在这些情况下,您可以在代码中触发断言以结束代码执行,并提供调试缺少或无效值的原因的机会。
使用断言的场合
整数下标索引传递到自定义下标实现,但下标索引值可能太低或太高。
一个值被传递给一个函数,但是一个无效的值意味着该函数不能完成它的任务。
当前是一个可选值nil,但非nil值对于后续代码成功执行至关重要。
1.可能越界的时候
2.可能为nil,nil的时候影响较大
3.从其他地方传递过来的值,可能不对对下边影响比较大。
使用方法
这里写图片描述
注释
断言模式在release阶段会自动关闭。

0 0
原创粉丝点击