NS_BLOCK_ASSERTIONS in Objective-C

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

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

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

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

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

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

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

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

NS_BLOCK_ASSERTIONS in Objective-C_第1张图片


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

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 (includingNSException.hwhich contains theNSAssertmacros) will get preprocessed before any other files in the project. A#define NS_BLOCK_ASSERTIONSin any of the project's header or implementation files therefore has no effect on theNSAssertmacros.

Try puttingNS_BLOCK_ASSERTIONSinto 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_ASSERTIONSinto 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


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

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

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

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

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

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

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

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

NS_BLOCK_ASSERTIONS in Objective-C_第2张图片


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

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 (includingNSException.hwhich contains theNSAssertmacros) will get preprocessed before any other files in the project. A#define NS_BLOCK_ASSERTIONSin any of the project's header or implementation files therefore has no effect on theNSAssertmacros.

Try puttingNS_BLOCK_ASSERTIONSinto 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_ASSERTIONSinto 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

你可能感兴趣的:(Objective-C)