Xcode - 基于 Target 导入不同的同名头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4868716/
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-14 20:09:57  来源:igfitidea点击:

Xcode - Importing different header file with same name based on Target

iphonexcodemacosiosheader-files

提问by NeilInglis

I have a project with multiple targets each of which builds a pretty similar versions of the app but with different images assets and plists. For plists/images that's fine but I use the ShareKit and Appirater frameworks which have header files with #defines for their config. For each version I believe need to import a different version of this header file, as the config is different for each app built by each target.

我有一个包含多个目标的项目,每个目标都构建了一个非常相似的应用程序版本,但具有不同的图像资产和 plist。对于 plists/images 这很好,但我使用 ShareKit 和 Appirater 框架,它们的头文件带有 #defines 作为其配置。对于每个版本,我认为需要导入此头文件的不同版本,因为每个目标构建的每个应用程序的配置都不同。

So target A has SHConfig.h and target B has a DIFFERENT SHConfig.h

所以目标 A 有 SHConfig.h,目标 B 有不同的 SHConfig.h

I could edit the source for these frameworks to import different headers based on the target but that'd be messy when I come to upgrade the frameworks.

我可以编辑这些框架的源代码以根据目标导入不同的标头,但是当我升级框架时会很麻烦。

Is there a better way to import different header files (with the same name) based on the target?

有没有更好的方法可以根据目标导入不同的头文件(同名)?

采纳答案by Peter Hosey

Assuming they're in different directories, set the Header Search Paths in each target to put the correct directory first.

假设它们位于不同的目录中,请在每个目标中设置标题搜索路径以首先放置正确的目录。

You may want to set it to something like $(SRCROOT)/foo:$(HEADER_SEARCH_PATHS), though I'm not sure whether that's necessary.

您可能希望将其设置为类似$(SRCROOT)/foo:$(HEADER_SEARCH_PATHS),但我不确定这是否有必要。

回答by Chris Prince

What I found useful was to put the Common directory name in the header search path, and then to use a different #import. My directory structure was Common/Views/v1 and Common/Views/v2. I wanted the v1 for one target and the v2 for another.

我发现有用的是将 Common 目录名称放在标题搜索路径中,然后使用不同的 #import。我的目录结构是 Common/Views/v1 和 Common/Views/v2。我想要一个目标使用 v1,另一个目标使用 v2。

In my case, the search path I used in Header Search Paths was:

就我而言,我在 Header Search Paths 中使用的搜索路径是:

$(SRCROOT)/../Common/

Then, I used:

然后,我使用了:

#import <Views/v2/ActivityIndicator.h>

In the target that needed the second version (this finds $(SRCROOT)/../Common/Views/v2/ActivityIndicator.h).

在需要第二个版本的目标中(这会找到 $(SRCROOT)/../Common/Views/v2/ActivityIndi​​cator.h)。

Oddly, the other target (the first one I created) is fine without specifying the full path. I.e.,

奇怪的是,另一个目标(我创建的第一个目标)没有指定完整路径就可以了。IE,

#import "ActivityIndicator.h"

works to find $(SRCROOT)/../Common/Views/v1/ActivityIndicator.h

可以找到 $(SRCROOT)/../Common/Views/v1/ActivityIndi​​cator.h

回答by Rishabh

Following process solved the issue for me

以下过程为我解决了这个问题

Select specific target Under "Build Phases" --> add "New Headers Phase" --> Expand "Headers" --> click on add(plus symbol) and --> browse to the file to be added specific for the target. (It will add file under 'project' section).

选择特定目标 在“构建阶段”下 --> 添加“新标题阶段” --> 展开“标题” --> 单击添加(加号)并 --> 浏览到要为目标添加的特定文件。(它将在“项目”部分下添加文件)。

Repeat the process for other targets. Tested on Xcode 10.2

对其他目标重复此过程。在 Xcode 10.2 上测试