You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode 13搭配CocoaPods如何全局禁用所有断言(含Debug模式)

你已经配置的ENABLE_NS_ASSERTIONS仅会禁用NSAssert等苹果Foundation框架提供的断言能力,你现在触发的是C标准库原生的assert()宏,该断言不受这个配置项控制,需要额外添加预处理宏禁用。

步骤1:更新Podfile配置

修改你的Podfile的post_install代码段,新增NS_BLOCK_ASSERTIONS预处理宏定义,这个宏是C标准库指定的断言开关,定义后所有assert()都会在编译期被置空:

post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
    # 原有Foundation断言配置
    config.build_settings['ENABLE_NS_ASSERTIONS'] = 'NO'
    # 新增C标准assert断言禁用配置
    existing_defs = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] || ['$(inherited)']
    unless existing_defs.include?('NS_BLOCK_ASSERTIONS=1')
      existing_defs << 'NS_BLOCK_ASSERTIONS=1'
    end
    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = existing_defs
  end
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      # 原有Foundation断言配置
      config.build_settings['ENABLE_NS_ASSERTIONS'] = 'NO'
      # 新增C标准assert断言禁用配置
      existing_defs = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] || ['$(inherited)']
      unless existing_defs.include?('NS_BLOCK_ASSERTIONS=1')
        existing_defs << 'NS_BLOCK_ASSERTIONS=1'
      end
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = existing_defs
    end
  end
end

步骤2:主项目添加对应配置

在你自己项目的项目配置、target配置的Build Settings > Preprocessor Macros选项中,给Debug、Release所有模式都添加NS_BLOCK_ASSERTIONS=1的宏定义。

步骤3:重新生成项目并清缓存

修改完Podfile后执行pod install重新生成Pods工程,之后打开Xcode按下Shift + Command + K清空编译缓存,再重新运行项目即可。

特殊情况说明

如果执行完以上操作断言仍触发,说明对应第三方库在代码内部强制覆盖了这个宏定义,或者使用了自定义的断言实现,你可以针对对应Pod单独添加预处理宏配置,或是fork该库修改断言逻辑。

内容的提问来源于stack exchange,提问作者Alessandro Ornano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 15:27:03