实际上,NSAssert 的意义何在?

What#39;s the point of NSAssert, actually?(实际上,NSAssert 的意义何在?)

本文介绍了实际上,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 的意义何在?

基础教程推荐