Xcode添加宏

时间:2018-11-30 03:00:28

标签: ios xcode cocoapods

我在Xcodeproj中为xcode项目(不是Pods目标)添加了宏,我在Podfile中尝试过这样。

 def add_macro
  require 'xcodeproj'
  project_path = './Nuclear.xcodeproj'
  project = Xcodeproj::Project.open(project_path)
  project.targets.each do |target|
    if target.name == 'Nuclear'
      target.build_configurations.each do |config|
        old_gcc_settings = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
        puts "⭕️target:#{target} config:#{config} GCC_PREPROCESSOR_DEFINITIONS:#{old_gcc_settings}"
        old_gcc_settings ||= ['$(inherited)']
        old_gcc_settings << 'XXX_TEST_MACRO=1'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = old_gcc_settings
        puts"target:#{target} config:#{config} GCC_PREPROCESSOR_DEFINITIONS:#{config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']}"
      end
    end
  end
end

pre_install do |installer|
  add_macro
end

当我运行pod install时,它的打印正确,但是项目目标没有要添加的宏。

有什么办法可以工作吗?

1 个答案:

答案 0 :(得分:0)

只需在方法project save的末尾添加add_macro。就像下面这样:

def add_macro
  require 'xcodeproj'
  project_path = './Nuclear.xcodeproj'
  project = Xcodeproj::Project.open(project_path)
  project.targets.each do |target|
    if target.name == 'Nuclear'
      target.build_configurations.each do |config|
        old_gcc_settings = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
        puts "⭕️target:#{target} config:#{config} GCC_PREPROCESSOR_DEFINITIONS:#{old_gcc_settings}"
        old_gcc_settings ||= ['$(inherited)']
        old_gcc_settings << 'XXX_TEST_MACRO=1'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = old_gcc_settings
        puts"target:#{target} config:#{config} GCC_PREPROCESSOR_DEFINITIONS:#{config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']}"
      end
    end
  end
  project.save
end

pre_install do |installer|
  add_macro
end