What#39;s the point of NSAssert, actually?(实际上,NSAssert 的意义何在?)
问题描述
我不得不问这个问题,因为:我唯一认识到的是,如果断言失败,应用程序就会崩溃.这就是使用 NSAssert 的原因吗?或者它还有什么好处?将 NSAssert 放在我在代码中所做的任何假设之上是否正确,例如一个永远不应该接收 -1 作为参数但可能是 -0.9 或 -1.1 的函数?
I have to ask this, because: The only thing I recognize is, that if the assertion fails, the app crashes. Is that the reason why to use NSAssert? Or what else is the benefit of it? And is it right to put an NSAssert just above any assumption I make in code, like a function that should never receive a -1 as param but may a -0.9 or -1.1?
推荐答案
断言是为了确保一个值是它应该是的.如果断言失败,则意味着出现问题,因此应用程序退出.使用 assert 的一个原因是,如果您有一些函数不会运行或会产生非常糟糕的副作用,如果传递给它的参数之一不完全是某个值(或值范围),您可以放置一个断言确保该值是您期望的值,如果不是,则确实有问题,因此应用程序退出.Assert 对于调试/单元测试非常有用,当你提供框架来阻止用户做邪恶"的事情时也是如此.
Assert is to make sure a value is what its supposed to be. If an assertion fails that means something went wrong and so the app quits. One reason to use assert would be if you have some function that will not behave or will create very bad side effects if one of the parameters passed to it is not exactly some value (or a range of values) you can put an assert to make sure that value is what you expect it to be, and if it's not then something is really wrong, and so the app quits. Assert can be very useful for debugging/unit testing, and also when you provide frameworks to stop the users from doing "evil" things.
这篇关于实际上,NSAssert 的意义何在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实际上,NSAssert 的意义何在?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01