xcode 为 cocoapod 依赖项指定预处理器宏,而不将其分叉

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17908569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:38:15  来源:igfitidea点击:

Specifying preprocessor macros for a cocoapod dependency, without forking it

xcodecocoapods

提问by matehat

I have an XCode workspace managed by Cocoapodwith a few dependencies to external libraries. One of them, MTDates, extends NSDateand NSDateComponentswith either prefixed methods, or non-prefixed if a certain preprocessor macro is defined (which is what I want).

我有一个 XCode 工作区,由Cocoapod一些对外部库的依赖项管理。其中之一,MTDates,扩展NSDateNSDateComponents带有前缀方法,或者如果定义了某个预处理器宏(这是我想要的),则不带前缀。

There are a few places where I can put the preprocessor macro definition in order to have the compiled library provide the non-prefixed methods, but all seem to be reset as soon as I ask Cocoapodto update the project, which leads me to think that these configs are driven by the pod spec. These include :

有几个地方我可以放置预处理器宏定义,以便编译库提供非前缀方法,但是一旦我要求Cocoapod更新项目,所有似乎都被重置,这让我认为这些配置由 pod 规范驱动。这些包括 :

  • The pod's target build settings
  • The pod's private .xcconfigfile in Cocoapod's Targets Support Files
  • pod 的目标构建设置
  • .xcconfigCocoapod 的Targets Support Files 中pod 的私有文件

Changing the pod's spec would require to manage my own version of the library, whereby losing the ability for cocoapodsto update it when a new version comes around. So my question is: is there a way to specify a preprocessor macro for a cocoapoddependency, without forking the pod and changing the pod's spec itself?

更改 pod 的规范需要管理我自己的库版本,从而cocoapods在新版本出现时失去更新它的能力。所以我的问题是:有没有办法为cocoapod依赖项指定预处理器宏,而无需分叉 pod 并更改 pod 的规范本身?

Edit:

编辑

There is an open issueabout this, that seem to be just around the corner.

有一个关于此的未决问题,似乎即将到来。

采纳答案by TWilly

This functionality is now available. Here is an example of what you could put at the bottom of your Podfile to add a macro based on a specific configuration.

此功能现已可用。下面是一个示例,说明您可以在 Podfile 底部添加基于特定配置的宏的内容。

post_install do |installer_representation|
  installer_representation.project.targets.each do |target|
    if target.name == "Pods-TweaksBuildConfigurationsDemo-Tweaks"
      target.build_configurations.each do |config|
        if config.name == 'QA'
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'FB_TWEAK_ENABLED=1']
        end
      end
    end
  end
end

回答by zhonglaoban

as you mentioned 'The pod's private .xcconfig file in Cocoapod's Targets Support Files', i do this resolved my problem.

正如您提到的“Cocoapod 的目标支持文件中的 pod 私有 .xcconfig 文件”,我这样做解决了我的问题。

s.xcconfig = {'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) COCOAPODS=1 IOS=1' }

回答by Kaan Dedeoglu

Looking at Cocoapods documentation, I don't think this is possible just yet, I think what you can do is to copy the pod spec - make the changes you want (e.g s.prefix_header_contents = #define symbolToDefine) and then add it to your local specs with a different name, and use that in your pod file. When a new version comes out unfortunately, you'd have to go in and change the tag number (and other stuff depending on the changes).

查看 Cocoapods 文档,我认为这还不可能,我认为您可以做的是复制 pod 规范 - 进行您想要的更改(例如s.prefix_header_contents = #define symbolToDefine),然后将其添加到您的本地规范中,并使用不同的名称,并在您的 pod 文件中使用它。当不幸出现新版本时,您必须进入并更改标签编号(以及其他取决于更改的内容)。