没有为CocoaPods目标定义DEBUG预处理macros

我遇到了一个叫DCIntrospect-ARC的问题,只能在DEBUG模式下工作。 它检查运行前是否定义了DEBUGmacros。 然而,它没有在CocoaPods目标中定义,即使我在Xcode中以debugging模式运行,它也无法运行,因为DEBUGmacros没有定义。

我可以在podspec中使用定义DEBUGmacros

s.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => '$(inherited) DEBUG=1' } 

但是这个定义了所有构buildconfiguration的DEBUG,而不仅仅是DEBUGconfiguration。

  1. 这是一个CocoaPods的问题? 一般不应该为豆荚定义DEBUGmacros?
  2. 我可以在Podspec文件中解决这个问题,只在debugging版本configuration中声明DEBUGmacros吗?

您可以使用Podfile中的post_install挂钩。

这个钩子允许你在生成的Xcode项目被写入磁盘之前做任何最后的修改,或者你可能想要执行的其他任务。 http://guides.cocoapods.org/syntax/podfile.html#post_install

 post_install do |installer_representation| installer_representation.pods_project.targets.each do |target| target.build_configurations.each do |config| if config.name != 'Release' config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1'] end end end end 

感谢John,我完成了我的自定义podfile脚本,该脚本还将优化级别更改为零并启用断言。

我有多个debuggingconfiguration(ACC和PROD),所以我需要更新几个属性进行debugging。

分享是关心,希望有人认为这有用。

 post_install do |installer| installer.project.build_configurations.each do |config| if config.name.include?("Debug") # Set optimization level for project config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0' # Add DEBUG to custom configurations containing 'Debug' config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)'] if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1' config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1' end end end installer.project.targets.each do |target| target.build_configurations.each do |config| if config.name.include?("Debug") # Set optimization level for target config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0' # Add DEBUG to custom configurations containing 'Debug' config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)'] if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1' config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1' end # Enable assertions for target config.build_settings['ENABLE_NS_ASSERTIONS'] = 'YES' config.build_settings['OTHER_CFLAGS'] ||= ['$(inherited)'] if config.build_settings['OTHER_CFLAGS'].include? '-DNS_BLOCK_ASSERTIONS=1' config.build_settings['OTHER_CFLAGS'].delete('-DNS_BLOCK_ASSERTIONS=1') end end end end end 

目前接受的答案不适用于Swift Pods。 这是对这个答案的一个单行的改变,似乎对两者都有效。

  post_install do |installer_representation| installer_representation.pods_project.targets.each do |target| target.build_configurations.each do |config| if config.name != 'Release' config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1'] config.build_settings['OTHER_SWIFT_FLAGS'] = ['$(inherited)', '-DDEBUG'] end end end end 

我认为接受的答案对我来说不太合适。 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']

||=用于分配一个空或零variables,但是如果config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']不是空的?

数组根本无法修改。 值为["POD_CONFIGURATION_PRODUCTION=1", "$(inherited)"]

所以我给了完整的anwser。

 post_install do |installer_representation| installer_representation.pods_project.build_configurations.each do |config| if config.name == 'Release' || config.name == 'Production' || config.name == 'Release-InHouse' config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [] config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] |= ['$(inherited)', 'NDEBUG=1'] end end end 

|| = []确保variables是一个有效的数组。 而arrayA |= arrayB意味着arrayA + arrayB并去掉重复的元素,然后返回到arrayA。

更容易:只要确保在xCode中的项目中有DEBUG=1macros,就可以在你的项目中使用GCC_PREPROCESSOR_DEFINITIONS,而不是释放模式。 如果将其添加到项目级别(而不是特定目标),它将被所有目标(debuggingtesting,自定义目标等)inheritance。 这是在新项目中默认设置的,通常预期会在那里。 如果你错过了,那可能会产生广泛的影响。

如果仍然无效,请确保您的GCC_PREPROCESSOR_DEFINITIONS的所有目标都有$(inherited) 。 CocoaPods和DEBUG都指望这一点。

设置