NS_BLOCK_ASSERTIONS in Objective-C

XCode debug 模式与 release 模式的一大重要区别:

debug 模式下,可以方便的进行调试,NSAssert 是暴力调试的一大利器

如果只使用一般的 if() {} 来进行 bug 的拣选的话,会存在一些比较麻烦的因素:

1。在你真正发布程序的时候,需要手动的去找到这些代码并将其注释掉

2。if(){} 和程序逻辑混在一堆,你找起来的时候是很麻烦的!

如果用到了 NSAssert 的话,你仅需要由 debug 转换为 release 模式,

所有的 断言将会被自动禁用掉,你不必再去手动的注释掉之前用来拣选 bug 的代码

省去了找寻的麻烦。同时,拣选bug 的 NSAssert 还在那儿,也方便你日后对程序进行升级~



有没有遇到过这样的情况:

1。有时侯在release模式下运行程序的时候,没有问题~ 切换到 debug 模式下,程序跑不通了

2。有时候在debug 模式下运行程序的时候,没有问题~ 切换到 release 模式下,程序跑不通了

为什么?

情况1的原因我找到一个:

debug模式下会试图将一些对象打印出来的。有些对象能够正常使用,但在打印的时候会出一些问题,导致bug;

情况2的原因也找到一个:

参考我前一篇转载的文章。


参考资料: http://stackoverflow.com/questions/6445222/ns-block-assertions-in-objective-c

I am using NSAssert() calls within an iPhone application and my understanding from the Apple docs is that assertions are not compiled into the code if NS_BLOCK_ASSERTIONS is defined.

To turn off assertions, in a header file I declare: #define NS_BLOCK_ASSERTIONS

However, the assert code still seems to run.

Is there something I am missing here?

Thanks

John

link | improve this question

77% accept rate
 
feedback

1 Answer

up vote 11 down vote accepted

If you created your Xcode project based on one of the standard templates, the Cocoa headers (including NSException.h which contains the NSAssert macros) will get preprocessed before any other files in the project. A #define NS_BLOCK_ASSERTIONS in any of the project's header or implementation files therefore has no effect on the NSAssert macros.

Try putting NS_BLOCK_ASSERTIONS into the preprocessor macros of your target or even project (for the release configuration only):

GCC_PREPROCESSOR_DEFINITIONS = NS_BLOCK_ASSERTIONS

Or put #define NS_BLOCK_ASSERTIONS into the precompiled header before the #import <Cocoa/Cocoa.h> or #import <Foundation/Foundation.h> lines.

link | improve this answer
 
I appreciate the thorough description provided, thank you! –  John  Jun 22 '11 at 20:16
2  
Note that in Xcode 4.2, the templates have this already set up for you: assertions are blocked in a Release build. –  matt  Nov 2 '11 at 15:45


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值