• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

NS_BLOCK_ASSERTIONS in Objective-C

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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
up vote11down voteaccepted

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):

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!JohnJun 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.mattNov 2 '11 at 15:45


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
up vote11down voteaccepted

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):

Or put#define NS_BLOCK_ASSERTIONSinto the precompiled header before the#import <Cocoa/Cocoa.h>or


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C继承和多态发布时间:2022-07-12
下一篇:
swift:OptionalType、Swift和Objective-C混编的讲解发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap