断言

  • Use NSAssert() and its companions.使用NSAssert()和它的伙伴。

    in the project define NS_BLOCK_ASSERTIONS for your release configuration.在项目定义为你的发行版配置的NS_BLOCK_ASSERTIONS。

    Xcode 4 tremplates disable NSAsserts in the release configuration. Xcode的4 tremplates发布配置中禁用NSAsserts 。 It adds它增加了

     -DNS_BLOCK_ASSERTIONS=1 

    to "Other C Flags" for "Release". “C标志”,“释放”。

    From the documentation:从文档:

    Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined.断言被禁用的预处理器宏NS_BLOCK_ASSERTIONS的如果被定义。

    The NSAssert macro evaluates the condition and serves as a front end to the assertion handler. NSAssert宏的情况进行评估,并断言处理程序的前端。

    Each thread has its own assertion handler, which is an object of class NSAssertionHandler.每个线程都有它自己的断言处理程序,这是一个类NSAssertionHandler对象。 When invoked, an assertion handler prints an error message that includes the method and class names (or the function name).断言处理程序被调用时,打印出错误消息,其中包括方法和类的名称(或函数名)。 It then raises an NSInternalInconsistencyException exception.它提出了一个NSInternalInconsistencyException的例外。 If condition evaluates to NO, the macro invokes handleFailureInMethod:object:file:lineNumber:description: on the assertion handler for the current thread, passing desc as the description string.如果条件的计算结果为NO,宏调用handleFailureInMethod的对象:文件:行号:说明:在当前线程的断言处理程序,通过递减的描述字符串。

    This macro should be used only within Objective-C methods.应该使用这个宏只在Objective-C的方法。

  • I will here provide a meta-answer:在这里,我将提供一元答案:

    Both @CocoaFu and @dasblinkenlight are correct. NS_BLOCK_ASSERTIONS turns offNSAssert() and NDEBUG turns off assert() . @ CocoaFu @ dasblinkenlight是正确的。NS_BLOCK_ASSERTIONS关闭NSAssert()NDEBUG关闭assert() You need both if you use both.你需要同时如果你同时使用。

  • Asserts are conditionally compiled out of your code when NDEBUG is defined.断言条件编译你的代码时NDEBUG被定义。 If you define NDEBUG=1 in the corresponding build settings section, you will deactivate asserts in your code regardless of the release or debug mode.如果你定义了NDEBUG=1相应的生成设置“一节中,你将取消断言在你的代码中,无论释放或调试模式。

原文地址:https://www.cnblogs.com/yingkong1987/p/3322494.html