Xcode - 自动将文件夹中的所有文件添加到目标

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

Xcode - Automatically add all files in a folder to a target

xcodebuildbuild-processbuild-automation

提问by Akshay Rawat

In Xcode, is there a way to specify that all files in a folder are compiled by a target. Eg. the 'Test' target automatically compiles all files in the 'Tests' folder, whereas the 'App' target compiles everything in the 'Sources' folder.

在 Xcode 中,有没有办法指定文件夹中的所有文件都由目标编译。例如。“Test”目标会自动编译“Tests”文件夹中的所有文件,而“App”目标会编译“Sources”文件夹中的所有文件。

Today, the way I'm doing it, is to add a file to a target every time I create it. This feels a bit error prone and redundant since the files are already organized in the correct folders.

今天,我的做法是,每次创建文件时,都将文件添加到目标中。这感觉有点容易出错和多余,因为文件已经组织在正确的文件夹中。

Thanks.

谢谢。

回答by Pavel

When you add files to Xcode project, you will have two options to choose from in files select dialog:

当您将文件添加到 Xcode 项目时,您将在文件选择对话框中有两个选项可供选择:

  1. Create groups for any added folders.
  2. Create folder references for any added folders.
  1. 为任何添加的文件夹创建组。
  2. 为任何添加的文件夹创建文件夹引用。

You need to select the second option. In this case, Xcode will always reflect the changes to files and subfolders of selected folder.

您需要选择第二个选项。在这种情况下,Xcode 将始终反映对所选文件夹的文件和子文件夹的更改。

It seems the question was asked a long time ago, but it's still on first place in google search, so hopefully it will be helpful for someone.

似乎这个问题很久以前就被问到了,但它仍然在谷歌搜索中排在第一位,所以希望它对某人有所帮助。

回答by HughHughTeotl

This is completely broken in Xcode. Adding folders by folder reference (@Pavel's answer) shouldbe correct - but it just doesn't work. See also https://stackoverflow.com/a/42600782/2518722. I'm using Xcode 8.3 (update: still broken in Xcode 11), but it's been broken for many generations prior, too.

这在 Xcode 中完全被破坏了。按文件夹引用添加文件夹(@Pavel 的答案)应该是正确的 - 但它不起作用。另见https://stackoverflow.com/a/42600782/2518722。我正在使用 Xcode 8.3(更新:在 Xcode 11 中仍然损坏),但它之前也已经损坏了很多代。

There isn't a perfect workaround, but this is what I do:

没有完美的解决方法,但这就是我所做的:

  1. Drag the directory into the project
  2. When asked, set options as follows:
    • Select Create groups for any added folder(ie, the opposite to what you think would be right)
    • DeselectCopy files if needed(because you want a link, not a copy).
  1. 将目录拖入项目中
  2. 当被问到时,设置选项如下:
    • 为任何添加的文件夹选择创建组(即,与您认为正确的相反)
    • 如果需要,取消选择复制文件(因为您需要链接,而不是副本)。

Now any changes to files in the shared directory will be reflected in Xcode, which is good.

现在对共享目录中文件的任何更改都会反映在 Xcode 中,这很好。

But, there's no way of refreshing the group. (Someone please tell me if I'm missing something obvious here.) So if you've added new files to the directory, you have to remove the group in Xcode and re-add it before those files show up. This is obviously inconvenient, but at least it's not too prone to user error - and is better than adding files manually one-by-one.

但是,没有办法刷新组。(如果我在这里遗漏了一些明显的东西,请有人告诉我。)因此,如果您向目录中添加了新文件,则必须在 Xcode 中删除该组并在这些文件出现之前重新添加它。这显然不方便,但至少它不太容易出现用户错误——而且比手动一个一个地添加文件要好。

Have to say I find it pretty unbelievable that such a simple and necessary feature of a development environment is so broken here.

不得不说,我觉得难以置信的是,开发环境的如此简单且必要的功能在这里被破坏了。

回答by Hlung

You can add multiple files to targets with this. But it requires some work in filtering what files you want.

您可以使用此将多个文件添加到目标。但是它需要一些工作来过滤你想要的文件。

Project > Target > Compile Sources > cmd+A to select all files > filter for .m files > click "Add"

项目>目标>编译源>cmd+A选择所有文件>过滤.m文件>点击“添加”

Similar to other resources,

与其他资源类似,

Project > Target > Copy Bundle Resources > filter for .png .wav .strings .xib .storyboard, etc. > click "Add"

项目 > 目标 > 复制捆绑资源 > 过滤 .png .wav .strings .xib .storyboard 等 > 单击“添加”

回答by ThomasW

Here are some other questions on stackoverflow attempting to deal with this issue:

以下是有关尝试处理此问题的 stackoverflow 的其他一些问题:

Add files to an Xcode project from a script?

将文件从脚本添加到 Xcode 项目?

Automatically adding generated source files to an xcode project

自动将生成的源文件添加到 xcode 项目中

回答by Moshe Rabaev

I just sort by type, so all my source files are listed consecutively.

我只是按类型排序,所以我所有的源文件都是连续列出的。

Then I hold Shift+Cmdand press the down arrow key to select all files I want.

然后我按住Shift+Cmd向下箭头键选择我想要的所有文件。

Once selected I can just check all to add to a specific target :)

选择后,我可以检查所有内容以添加到特定目标:)

回答by Motti Shneor

As far as I know, there is no bug in Xcoderegarding this, and Xcode never supportedsuch a mechanism (automatically adding/removing sources from a target based on availability in file-system).

据我所知,Xcode 中没有关于此的错误,并且 Xcode从未支持这种机制(根据文件系统中的可用性自动从目标添加/删除源)。

The Group variations (groups-representing-a-folder vs. "regular" groups) merely allow you flexibility in how your project tree looksvisually - should it closely follow your repository on disk, or be more "human-readable" and descriptive - regardless of the placement of sources in the repository.

该集团的变化(组-代表-一个文件夹与“常规”组)只允许你怎么项目树灵活性外观视觉-它应该紧跟磁盘上的存储库,或更“人类可读”的描述-无论源在存储库中的位置如何。

I'm speaking of all Xcode versions I worked with - i.e. v1.0b2 to v11.3

我说的是我使用过的所有 Xcode 版本 - 即 v1.0b2 到 v11.3

In any way, I admit it WOULD BE NICE to have such a mechanism as a new feature (we could pledge Apple for this).

无论如何,我承认拥有这样一种机制作为新功能会很好(我们可以为此向 Apple 承诺)。

A way to do it now, could be to build a tiny new "Compiler" whose input is a Folder path, and whose "Generated code" would be the set of source files in that folder. If we add such a 'compiler' as a build rule to our target --- we might achieve the desired effect.

现在的一种方法是构建一个小型的新“编译器”,其输入是文件夹路径,其“生成的代码”将是该文件夹中的源文件集。如果我们将这样的“编译器”作为构建规则添加到我们的目标中——我们可能会达到预期的效果。

If you want to dig in this, follow the way Protobuf can be added into Xcode projects, and how CoreML models are handled in an Xcode target.

如果您想深入了解,请遵循将 Protobuf 添加到 Xcode 项目的方式,以及如何在 Xcode 目标中处理 CoreML 模型。

回答by justin

One approach you could take is to use CMake to generate the Xcode target for you.

您可以采用的一种方法是使用 CMake 为您生成 Xcode 目标。

Detail: CMake - Automatically add all files in a folder to a target?

详细信息:CMake - 自动将文件夹中的所有文件添加到目标?

Another potential avenue would be to configure a custom build phase.

另一个潜在的途径是配置自定义构建阶段。

回答by Paul R

Yes - just select all the files in the folder and use the inspector window (targets tab).

是的 - 只需选择文件夹中的所有文件并使用检查器窗口(目标选项卡)。

回答by regulus6633

Xcode is extensible using shell scripts. You can set the shell script to run at build time so you can basically make xcode do anything you want. You're only limited by your shell scripting abilities and I'm sure you can find help for that too.

Xcode 可以使用 shell 脚本进行扩展。您可以将 shell 脚本设置为在构建时运行,这样您基本上可以让 xcode 做您想做的任何事情。您只受到 shell 脚本能力的限制,我相信您也可以找到帮助。