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

发布于 2024-09-01 20:20:25 字数 180 浏览 7 评论 0原文

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

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

谢谢。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

冷了相思 2024-09-08 20:20:25

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

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

  1. 将目录拖到项目中
  2. 当询问时,设置选项如下:
    • 选择为任何添加的文件夹创建组(即与您认为正确的相反)
    • 取消选择 根据需要复制文件(因为您需要链接,而不是副本)。

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

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

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

This is completely broken in Xcode. Adding folders by folder reference (@Pavel's answer) should be 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.

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)
    • Deselect Copy files if needed (because you want a link, not a copy).

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

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.

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

暮年 2024-09-08 20:20:25

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

  1. 为任何添加的文件夹创建组。
  2. 为任何添加的文件夹创建文件夹引用。

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

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

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

  1. Create groups for any added folders.
  2. Create folder references for any added folders.

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

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.

烏雲後面有陽光 2024-09-08 20:20:25

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

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

与其他资源类似,

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

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"

Similar to other resources,

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

ぃ弥猫深巷。 2024-09-08 20:20:25

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

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

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

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

Add files to an Xcode project from a script?

Automatically adding generated source files to an xcode project

旧时光的容颜 2024-09-08 20:20:25

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

组变体(代表文件夹的组与“常规”组)仅允许您灵活地选择项目树的视觉外观 - 它是否应该紧密遵循磁盘上的存储库,或者更多“人类可读的”和描述性的——无论源在存储库中的位置如何。

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

无论如何,我承认拥有这样一个新功能的机制会很好(我们可以向 Apple 保证这一点)。

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

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

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

The Group variations (groups-representing-a-folder vs. "regular" groups) merely allow you flexibility in how your project tree looks visually - 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

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

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.

守望孤独 2024-09-08 20:20:25

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

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

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

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

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

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

浅唱々樱花落 2024-09-08 20:20:25

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

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

毅然前行 2024-09-08 20:20:25

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

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

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

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

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

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

私藏温柔 2024-09-08 20:20:25

Xcode 可使用 shell 脚本进行扩展。您可以将 shell 脚本设置为在构建时运行,这样您基本上就可以让 xcode 执行您想要的任何操作。您仅受 shell 脚本编写能力的限制,我相信您也可以找到这方面的帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文