NS_BLOCK_ASSERTIONS in Objective-C

NS_BLOCK_ASSERTIONS in Objective-C,第1张

概述XCode debug 模式与 release 模式的一大重要区别: debug 模式下,可以方便的进行调试,NSAssert 是暴力调试的一大利器 如果只使用一般的 if() {} 来进行 bug 的拣选的话,会存在一些比较麻烦的因素: 1。在你真正发布程序的时候,需要手动的去找到这些代码并将其注释掉 2。if(){} 和程序逻辑混在一堆,你找起来的时候是很麻烦的! 如果用到了 NSAssert

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


4
down vote favorite 1 share [g+] share [fb] share [tw]

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

iphone  objective-c  xcode  debugging
link | improve this question asked  Jun 22 '11 at 19:11

John
1,145 2 13 23
77% accept rate
  Feedback
1 Answer active oldest votes
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):

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

| improve this answer edited  Jun 22 '11 at 19:53
answered  Jun 22 '11 at 19:30

puzzle
3 9
 
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

分享到:  总结

以上是内存溢出为你收集整理的NS_BLOCK_ASSERTIONS in Objective-C全部内容,希望文章能够帮你解决NS_BLOCK_ASSERTIONS in Objective-C所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1061258.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存