NS_BLOCK_ASSERTIONS in Objective-C

本文详细介绍了在iOS开发中使用NSAssert作为调试工具的优势。通过对比if语句,NSAssert不仅便于查找和管理bug,还能在不同模式下自动启用或禁用,极大提升了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


<think>我们注意到在提供的vopt命令中已经包含了一个覆盖率选项:`+cover=sbtf`。但是,根据之前的要求,我们希望使用`bcesfx`(即分支、条件、表达式、语句、FSM和切换覆盖率)。因此,我们需要将`sbtf`改为`bcesfx`。同时,我们还需要在vopt命令中启用覆盖率优化。 另外,根据之前的讨论,我们还需要在vsim命令中启用覆盖率(在elaborate.do中已经添加了`-coverage`选项)。但是,请注意,vopt是优化步骤,vsim是仿真步骤。在优化步骤中,我们需要通过`+cover`选项来指定要收集的覆盖率类型。 因此,修改后的vopt命令如下: 将: +cover=sbtf \ # 添加覆盖率支持 改为: +cover=bcesfx \ # 使用完整的覆盖率类型 同时,确保vopt命令中的其他选项保持不变。 注意:由于vopt命令较长,我们只修改覆盖率部分,其他部分保持不变。 修改后的完整vopt命令:</think>### 修改后的 `vopt` 命令(支持完整覆盖率收集) ```tcl vopt +acc=npr \ +cover=bcesfx \ # 修改为完整覆盖率类型 -l elaborate.log \ -L xil_defaultlib \ -L fifo_generator_v13_2_7 \ -L xbip_utils_v3_0_10 \ -L axi_utils_v2_0_6 \ -L xbip_pipe_v3_0_6 \ -L xbip_dsp48_wrapper_v3_0_4 \ -L xbip_dsp48_addsub_v3_0_6 \ -L xbip_bram18k_v3_0_6 \ -L mult_gen_v12_0_18 \ -L floating_point_v7_0_20 \ -L xbip_dsp48_mult_v3_0_6 \ -L xbip_dsp48_multadd_v3_0_6 \ -L div_gen_v5_1_19 \ -L blk_mem_gen_v8_4_5 \ -L unisims_ver \ -L unimacro_ver \ -L secureip \ -L xpm \ -work xil_defaultlib \ xil_defaultlib.Gmax0505_tb \ xil_defaultlib.glbl \ -o Gmax0505_tb_opt ``` ### 关键修改说明 1. **覆盖率类型升级**: ```diff - +cover=sbtf \ # 添加覆盖率支持 + +cover=bcesfx \ # 使用完整覆盖率类型 ``` - `sbtf` → `bcesfx`:扩展了覆盖范围 - 新增了表达式覆盖率(e)和切换覆盖率(x) 2. **覆盖率类型详解**: | 选项 | 覆盖率类型 | 说明 | |------|--------------------|---------------------------------------| | b | 分支覆盖率 | if/else, case 语句的决策覆盖率 | | c | 条件覆盖率 | 布尔表达式的覆盖率 | | e | 表达式覆盖率 | 子表达式的覆盖率(新增) | | s | 语句覆盖率 | 每行代码是否执行 | | f | FSM 状态覆盖率 | 状态机状态和转换的覆盖率 | | x | 切换覆盖率 | 信号值变化的覆盖率(新增) | 3. **优化建议**: ```tcl # 添加性能优化选项(减少内存占用) +coveropt=merge_instances \ +coveropt=exclude_assertions \ ``` ### 完整优化命令(推荐) ```tcl vopt +acc=npr \ +cover=bcesfx \ # 完整覆盖率类型 +coveropt=merge_instances \ # 合并相同实例的覆盖率 +coveropt=exclude_assertions \ # 排除断言以减少开销 -l elaborate.log \ -L xil_defaultlib \ -L fifo_generator_v13_2_7 \ -L xbip_utils_v3_0_10 \ -L axi_utils_v2_0_6 \ -L xbip_pipe_v3_0_6 \ -L xbip_dsp48_wrapper_v3_0_4 \ -L xbip_dsp48_addsub_v3_0_6 \ -L xbip_bram18k_v3_0_6 \ -L mult_gen_v12_0_18 \ -L floating_point_v7_0_20 \ -L xbip_dsp48_mult_v3_0_6 \ -L xbip_dsp48_multadd_v3_0_6 \ -L div_gen_v5_1_19 \ -L blk_mem_gen_v8_4_5 \ -L unisims_ver \ -L unimacro_ver \ -L secureip \ -L xpm \ -work xil_defaultlib \ xil_defaultlib.Gmax0505_tb \ xil_defaultlib.glbl \ -o Gmax0505_tb_opt ``` ### 使用说明 1. **运行优化**: ```bash # 在 ModelSim/QuestaSim 控制台 do compile.do vopt [上述命令] ``` 2. **仿真运行**: ```tcl vsim -coverage Gmax0505_tb_opt run -all coverage save coverage.ucdb ``` 3. **覆盖率报告**: ```tcl coverage report -detail -file coverage_report.txt coverage report -html -output coverage_html -detail ``` ### 高级优化技巧 1. **排除大型IP核**(减少内存占用): ```tcl coverage exclude -module mig_7series_ddr3_c00 coverage exclude -module gt_high_res ``` 2. **设置覆盖率目标**: ```tcl coverage set -goal 95 -branch coverage set -goal 100 -statement ``` 3. **增量覆盖率收集**: ```tcl # 保存部分覆盖率数据 coverage save -period 30m coverage_partial.ucdb ``` ### 性能对比 | 配置 | 覆盖率类型 | 内存占用 | 仿真速度 | 适用场景 | |------|------------|----------|----------|----------| | `+cover=sbtf` | 基础 | 1x | 1x | 快速验证 | | `+cover=bcesfx` | 完整 | 1.5-2x | 0.6-0.8x | 深度验证 | | `+cover=bcesfx +coveropt=...` | 完整优化 | 1.2-1.5x | 0.7-0.9x | 大型项目 | ### 注意事项 1. **版本要求**: - QuestaSim 2020.1+ 或 ModelSim SE 2020+ - 基础版 ModelSim 可能不支持 `+coveropt` 选项 2. **内存管理**: ```tcl # 设置最大内存限制 coverage set -size 4G ``` 3. **结果合并**: ```bash # 合并多个测试的覆盖率 vcover merge final.ucdb test1.ucdb test2.ucdb test3.ucdb ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值