NSAsert的实际意义是什么? [英] What's the point of NSAssert, actually?

查看:333
本文介绍了NSAsert的实际意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须问这个,因为:我唯一认识的是,如果断言失败,应用程序崩溃。这是为什么要使用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 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.

这篇关于NSAsert的实际意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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