Xcode 可以使用“文件夹引用”作为代码吗?

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

Can Xcode Use "Folder References" for Code?

xcodexcode4

提问by Dan Rosenstark

Like many people, I would love to have Xcode use a folder structure that mirrors the folder-structure on disk. However, I cannot get the code in "folder references" (the cyan folders) to show up in my project targets under "Compile Sources." Is there any way to do this?

像许多人一样,我希望 Xcode 使用一个文件夹结构来反映磁盘上的文件夹结构。但是,我无法让“文件夹引用”(青色文件夹)中的代码显示在“编译源”下的项目目标中。有没有办法做到这一点?

I have managed to even add a cyan folder to the "Compile Sources" build phase, but that does not result in the contents of that folder being added.

我什至设法将青色文件夹添加到“编译源”构建阶段,但这不会导致添加该文件夹的内容。

How can I use folder references for code?

如何将文件夹引用用于代码?

采纳答案by William Cerniuk

The simple (and very unfortunate) answer is that Folder References under Xcode remain broken and buggy, they don't work. Tested 04-Mar-2017 in Xcode 8.2.1

简单(而且非常不幸)的答案是 Xcode 下的文件夹引用仍然损坏和错误,它们不起作用。2017 年 3 月 4 日在 Xcode 8.2.1 中测试

Below is an example exploration so you do not have to waste your time replicating the Xcode failure.

下面是一个示例探索,因此您不必浪费时间复制 Xcode 故障。

(incidentally buggy Xcode crashed twice while I was producing this example)

(顺便说一下,在我制作这个例子时,有问题的 Xcode 崩溃了两次)

Per the question, the overall desire is to use a folder reference in Xcode so Xcode picks up all the files in the folder and includes them in the project, and by proxy will update automatically based upon any Finder or Xcode changes of the folder. In this way 50 projects all leveraging the same set of common source code files do not have to be individually updated when those folders get changed. Below explores how this is broken in Xcode (tested in 8.2.1)

根据问题,总体希望是在 Xcode 中使用文件夹引用,以便 Xcode 选择文件夹中的所有文件并将它们包含在项目中,代理将根据文件夹的任何 Finder 或 Xcode 更改自动更新。这样,当这些文件夹发生更改时,50 个项目都利用相同的公共源代码文件集,而不必单独更新。下面探讨了这是如何在 Xcode 中被破坏的(在 8.2.1 中测试)

The example: ViewController.mincludes NSError+Core.hso we want everything from the folder "NSError+Core" to be added to the project.

示例:ViewController.mincludesNSError+Core.h所以我们希望将文件夹“NSError+Core”中的所有内容添加到项目中。

enter image description here

在此处输入图片说明

NSError+Core.his in this centrally located development folder

NSError+Core.h在这个位于中心的开发文件夹中

enter image description here

在此处输入图片说明

Drag the source folder from the Finder into the Project under the "Support" group (nothing up my sleeves, simple drag)

将源文件夹从 Finder 拖到“支持”组下的项目中(无所事事,简单拖动)

enter image description here

在此处输入图片说明

Xcode dutifully offers to add the drag to the target, note the "Create folder references" is selected, not "Create group references". Note also it is clear that Xcode is offering and is told to add this folder and files to the targets.

Xcode 尽职尽责地将拖动添加到目标,注意选择了“创建文件夹引用”,而不是“创建组引用”。另请注意,很明显 Xcode 提供并被告知将此文件夹和文件添加到目标。

enter image description here

在此处输入图片说明

Although everything looks like it should work, the compiler does not pick up the header file and a recompile yields the same results... can't find header. Ditching the DerivedData does not help either.

尽管一切看起来都可以正常工作,但编译器没有选择头文件,重新编译会产生相同的结果……找不到头文件。放弃 DerivedData 也无济于事。

enter image description here

在此处输入图片说明

So doing a double check, we check the "Compile Sources" under the project and sure enough, the source file is not there either. Remember, Xcode 'added' it to the target...

所以仔细检查,我们检查了项目下的“编译源”,果然,源文件也不在那里。请记住,Xcode 将其“添加”到目标...

enter image description here

在此处输入图片说明

So what if we drag it from the folder into the "Support" group...

那么如果我们将它从文件夹中拖到“支持”组中会怎样...

enter image description here

在此处输入图片说明

It offers to add them to the project again?! Note that the settings are identical to the first time they were drug in by virtue of the parent folder drag instead of files...

它提供再次将它们添加到项目中?!请注意,由于父文件夹拖动而不是文件,这些设置与它们第一次被吸毒时相同......

enter image description here

在此处输入图片说明

And nowthe source file shows up in the "Compile Sources" list. Note the bizarre double listing of the files in the project.

现在的源文件显示了在“编译来源”列表中。请注意项目中文件的奇怪双重列表。

(Xcode crashed shortly after snapping this screen shot)

(Xcode 在截取此屏幕截图后不久就崩溃了)

enter image description here

在此处输入图片说明

And of course the compiler can now find the header file and the error clears on the import as it should have the first time we drug it in...

当然,编译器现在可以找到头文件,并且错误会在导入时清除,因为它应该在我们第一次使用时...

enter image description here

在此处输入图片说明

Did it just need a little help to "find" the file? If so, the "Create folder references" does exactly what?

“找到”文件只需要一点帮助吗?如果是这样,“创建文件夹引用”究竟是做什么的?

So we try and tidy up and drag the files back from the parent "Supporting Files" group to their rightful folder. Without any confirmation, indication, notification, the files just vanish from the group and nothing happens in the NSError+Core folder.

因此,我们尝试整理并将文件从父“支持文件”组拖回其合法文件夹。没有任何确认、指示、通知,文件就会从组中消失,而 NSError+Core 文件夹中没有任何反应。

enter image description here

在此处输入图片说明

Oh by the way, it really did delete them from the project too... The Compile Sources no longer has the NSError+Core.m reference.

哦,顺便说一句,它确实也将它们从项目中删除了......编译源不再有 NSError+Core.m 引用。

enter image description here

在此处输入图片说明

SO to sum up, "Folder references" as implemented thus far do not seem to have any useful purpose... It would appear to be a 6+ year old dunsel on the USS Xcode.

总而言之,迄今为止实施的“文件夹引用”似乎没有任何有用的目的......它似乎是 USS Xcode 上的一个 6 岁以上的傻瓜。

回答by rbgrn

Kevin - linked source folders, folder references, etc, are super useful for when you have a common code base shared across different IDEs, like I do for my games that I compile on Windows, Mac, iOS, Android, Linux, etc. I have 3 different IDEs all building the same shared codebase, so it's very helpful when one automatically picks up on a new file and adds it right into the project after I merely run svn update, and I svn commit from one IDE (say XCode) and my Eclipse in Windows project picks up the change. I have a different project for each because each IDE likes the project files in a certain configuration so it's easier for me to have multiple SVN directories (base-project, project-ios, project-android) that all share code in base-project than to have one mega project directory with the different IDE bits unhappily all shoved into subdirectories (which is what I tried the first time around).

Kevin - 链接的源文件夹、文件夹引用等对于在不同 IDE 之间共享公共代码库时非常有用,就像我在 Windows、Mac、iOS、Android、Linux 等上编译的游戏一样。我有 3 个不同的 IDE,它们都构建了相同的共享代码库,因此当我仅运行 svn update 并且我从一个 IDE(例如 XCode)和我在 Windows 项目中的 Eclipse 接受了更改。我为每个项目都有一个不同的项目,因为每个 IDE 都喜欢特定配置中的项目文件,因此我更容易拥有多个 SVN 目录(base-project、project-ios、

Furthermore - it used to work fine in XCode 3. They seemed to not like that useful of a feature so it is no longer working in XCode 4 as I've just found out.

此外 - 它曾经在 XCode 3 中工作正常。他们似乎不喜欢一个有用的功能,所以我刚刚发现它不再在 XCode 4 中工作。

回答by sixstringtheory

I just tried doing this to share code across multiple Xcode projects, and our team came to the conclusion that it's better to create an Xcode project that contains all of your shared classes, compiles them into a static/dynamic library, and then add that as a subproject to those that need the shared code. Then you can set up target dependencies and link your shared library. This will get you the "automatic updating" every time you add a new class to the shared library project.

我只是尝试这样做以在多个 Xcode 项目之间共享代码,我们的团队得出的结论是,最好创建一个包含所有共享类的 Xcode 项目,将它们编译为静态/动态库,然后将其添加为需要共享代码的人的子项目。然后您可以设置目标依赖项并链接您的共享库。每次将新类添加到共享库项目时,这将使您获得“自动更新”。

This approach also works well with submodules or even cocoapods/carthage.

这种方法也适用于子模块甚至 cocoapods/carthage。

回答by Lily Ballard

You can't. Why are you even trying? A folder reference's job is to embody a folder, without having entries for all the individual files in the folder. It's primary use is for copying an entire folder of resources verbatim into a project. If you want to compile the sources though, then those sources must be referenced in the Compile Sources build phase of the target, which requires having individual entries for each file.

你不能。你为什么还要努力?文件夹引用的工作是包含一个文件夹,而不是文件夹中所有单个文件的条目。它的主要用途是将整个资源文件夹逐字复制到项目中。如果你想编译源代码,那么这些源代码必须在目标的编译源代码构建阶段被引用,这需要每个文件都有单独的条目。

Depending on what's in the folder, it might make more sense to have a Makefile or some other external build process that builds the content in the folder into a static library. This way you can invoke that build process from a Shell Script Phase, and then just link in the resulting static library. Alternatively if you're using this folder as a way to have a shared bit of code (e.g. an svn:externals or git submodule), you could give that folder its own Xcode project and then embed that project into any of your other projects which share this folder.

根据文件夹中的内容,使用 Makefile 或其他一些外部构建过程将文件夹中的内容构建到静态库中可能更有意义。通过这种方式,您可以从 Shell 脚本阶段调用该构建过程,然后只需链接生成的静态库。或者,如果您将此文件夹用作共享代码位的一种方式(例如 svn:externals 或 git 子模块),您可以为该文件夹提供自己的 Xcode 项目,然后将该项目嵌入到您的任何其他项目中共享这个文件夹。