Xcode组和文件夹引用之间有什么区别? [英] What is the difference between an Xcode Group and a Folder Reference?

查看:105
本文介绍了Xcode组和文件夹引用之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在StackOverflow上的许多地方,已经提出并回答了这个问题。但是,我发现其中的大多数虽然在技术上是正确的,但遗漏了一些特定的细节,这些细节不仅可以解释实际发生的情况,而且在尝试调试文件引用或生成问题时可能是有价值的信息。

Many places here on StackOverflow, this question has been asked and answered. However, I have found most of those, while technically correct, leave out some specific details that not only explain what's actually going on, but can be valuable information when trying to debug file references or build issues.

我决定根据Jeff Atwood的关于鼓励发表自己的答案的自己的评论在他说的地方...

I decided to post this here per Jeff Atwood's own comments on encouraging posting your own answers where he says...


询问并回答您自己的问题,我们特别鼓励我一直这样做!

It is not merely OK to ask and answer your own question, it is explicitly encouraged [...] I do it all the time!

因此,我要详细解释这些项目在幕后会发生什么,希望这些知识可以帮助他人,因为它帮助到了我, 尤其是在消除常见的误解方面从Xcode 9开始,项目树会镜像磁盘上的文件夹,实际上并非如此。

As such, I'm adding a detailed explanation of what exactly happens under the hood with these items hoping this knowledge can help others as it has helped me, especially in regards to clearing up the all-too-common misconception of thinking as of Xcode 9, the project tree mirrors the on-disk folders, which isn't actually the case.

推荐答案

深入探讨组和文件夹引用


如上所述,关于此主题,这里有很多关于SO的答案,所有这些都解释了基本差异。但是,在行为和了解实际发生的事情时,有一些细微的细节会被忽略,当您的项目或其更改未按预期进行时,这可能会导致令人沮丧的体验。因此,我将通过更详尽的说明将帽子戴上戒指。希望这将有助于避免很多头痛。

A Deep-Dive Into Groups and Folder References

As mentioned above, there are lots of answers here on SO around this topic, all which explain the basic differences. However, there are some subtle details that are omitted when it comes to behaviors and understanding of what's actually going on which can lead to a frustrating experience when your project or its changes aren't working as expected. As such, I'm tossing my hat into the ring with a more thorough explanation. Hopefully this will help avoid a lot of headaches.

现在,让我们动手吧!

文件夹引用更易于掌握。它们是指向实际磁盘文件夹的简单指针。磁盘上文件夹中的所有内容都会出现在项目树中。

Folder references are simpler to grasp. They are simple pointers to actual on-disk folders. Whatever's in the folder on-disk will appear in the project tree.

您可以通过图标标识文件夹引用,该图标将变为蓝色。

You identify a folder reference by its icon, which will be blue.

通过在项目树中选择文件夹引用,然后选择将其包含在检查器中的目标,可以在应用程序的该文件夹中包含任何内容。

You can include whatever is in that folder in your application by selecting the folder reference in your project tree, then choosing which target to include it in in the inspector.

注意:您只能选择目标位于文件夹引用级别,而不是单个文件级别。

Note: You can only choose the target at the Folder Reference level, not the individual file level. It's an all-or-nothing addition.

一个重要的副作用是,当您可以从文件夹引用中将资源捆绑到应用程序中时,您不能构建源代码文件夹参考中的代码。源代码必须成组!

An important side-effect is while you can bundle resources into your application from a folder reference, you CANNOT build source code from a folder reference. Source code must be in groups!

另一方面,组显然更复杂。在最简单的解释中,它们是Xcode项目中的仅逻辑容器,用于存储和组织指向源代码文件的相对指针。这些仅存储在Xcode项目中,而不存储在磁盘上。

Groups on the other hand are deceivingly more complex. In their simplest explanation, they are logical-only containers in your Xcode project that you use to store and organize relative pointers to your source code files. These are stored in the Xcode project only, not on disk.

您可以通过黄色文件夹标识项目中的组。

You can identify a group in your project by a yellow folder.

每个项目,无论是组本身还是组内的文件引用都指定了位置以及它与项目结构的关系。可以在检查器的身份和类型部分中找到。

Each item, whether a group itself or a file-reference within the group specifies a location and how it is related to the project's structure. This can be found in the 'Identity and Type' section of the inspector.

注意...


  • 可以 引用磁盘上的特定文件夹, 但不必

  • 文件引用 必须 指向磁盘上的特定文件。

  • Groups can reference a specific folder on disk, but don't have to
  • File references must point to a specific file on disk.

引用的存储取决于位置的值:

How those references are stored depends on the value for 'Location':


  • 如果位置设置为绝对路径,则引用将包含整个

  • 如果将其设置为相对于组,则它首先确定该组当前要解析的内容,然后相对于该值存储引用。

在图像上还有细微的差别。如果该组指向实际文件夹,您将只看到黄色文件夹。但是,如果该组未引用特定的文件夹(即,它仅是逻辑文件夹),则该文件夹的图标中将显示一个小三角形。

There's also a subtle difference in the iconography. If the group points to an actual folder, you will just see the yellow folder. If however, the group doesn't reference a specific folder (i.e. it is logical-only) you will see a small triangle in the folder's icon.

此外,无论该组是否为无论是否连接,您都可以通过查看检查器中的全路径属性来始终查看组或其子级所引用的当前物理位置。

Additionally, regardless of whether the group is connected or not, you can always see what the current physical location referenced by a group or any of its children by looking at the 'Full Path' property in your inspector.


同样,重要的是要注意,您的逻辑组结构与物理磁盘结构 无关 人们经常会犯错以Xcode 9开头的思维,这种说法并不正确,但请继续阅读以了解为什么仍然存在这种误解。

如上所述,组并不代表实际的磁盘文件夹。它们是纯粹用于代码组织的简单逻辑分组。但是,它们可以指向到实际的磁盘文件夹。您可以通过检查器身份和类型部分中位置下拉菜单下的小文件夹按钮设置哪个文件夹。您还可以通过按圆圈中的小x来再次清除该关联。

As mentioned, groups do not represent actual on-disk folders. They are simple logical groupings purely for code organization. However, they can point to an actual on-disk folder. You set which folder that is via the little folder button under the Location dropdown in the 'Identity and Type' section of the inspector. You can also clear that association again by pressing the little x in the circle.

现在这是要获取的棘手部分。当处理不指向实际文件夹的组时,它们的全部目的只是为了帮助您作为用户以对您有意义的结构组织代码,并使Xcode能够解析磁盘上的实际路径,从而这些文件引用。

Now here's the tricky part to get. When dealing with groups that aren't pointing to actual folders, their entire purpose is simply to help you as a user organize your code in a structure that makes sense to you, and for Xcode to be able to resolve the actual paths on disk to those file references.

但是,如果您有一个指向的文件夹,该怎么办?现在,一套全新的规则开始发挥作用。这些规则是Xcode 9中的更改。

But what if you have a group that does point to a folder? Well now an entire new set of rules comes into play. These rules are what changed in Xcode 9.

在Xcode 9之后,修改组的名称可以修改磁盘上的名称,但不一定。如果组名和物理文件夹名称​​ 在重命名之前匹配 ,它将这样做。如果没有,则它们出于所有目的和目的都是断开连接。

After Xcode 9, modifying a group's name may modify the on-disk name, but not necessarily. It will only do so if the group name and physical folder name match prior to the rename. If they don't, they are for all intents and purposes 'disconnected'.

例如,假设您的项目中有一个名为虚拟的组,该组指向以下文件夹:像这样的名为Literal的磁盘...

For instance, say you have a group in your project called 'Virtual' that points to a folder on-disk called Literal, like so...

Group      On-disk Folder
-----      --------------
Virtual -> Literal

如果将虚拟组重命名为概念,则磁盘上没有任何反应。

If you rename the group 'Virtual' to 'Conceptual', nothing happens on-disk.

Conceptual -> Literal

如果您随后将'Conceptual'重命名为'Literal',则它与磁盘上的实际文件夹名称匹配...

If you then rename 'Conceptual' to 'Literal' so it matches the actual folder name on disk...

Literal -> Literal

...然后再次将文字重命名为已更改,组

...then again rename 'Literal' to 'Changed', both the group and the folder will be updated to 'Changed'.

Changed -> Changed



注意:这与 where 无关文件夹在磁盘上。

Note: This has nothing to do with where the folder is on-disk. This is only pertaining to the name itself, nothing else.

我们要去哪里?

关于它在磁盘上的位置,事情也比看起来复杂得多。如果移动当前未指向实际磁盘文件夹的组,则Xcode会更新其将项目的相对路径存储在项目文件中的方式,从而使它们相对于更新的组结构,而不会发生任何事情。

As for where it is on disk, there too things are more complex than they seem. If you move a group that is not currently pointing to an actual on-disk folder, nothing happens except Xcode updates how it stores your project's items' relative paths in the project file, making them relative to the updated group structure.

但是,如果您移动当前指向文件夹的组- ,即使其名称与磁盘上的文件夹不匹配 (是一个关键点,并且经常引起有关损坏的项目树的混乱)-它指向的物理磁盘文件夹将相对于您拖到其下的任何组以及其中的所有项目移动到新位置。磁盘上 是否在项目中引用了该文件夹!

However if you move a group that is currently pointing to a folder--even if its name doesn't match the folder on disk (that is a critical point and an oft-source of confusion about 'corrupted' project trees)--the physical on-disk folder it points to will be moved to the new location relative to whatever group you drag it under, along with all items in that folder on disk whether referenced in your project or not!

例如,假设您在项目结构中有此文件夹。

For instance, say you have this in your project structure...

Project
   GroupA -> Points to \Code\Project\GroupA
   GroupB -> Points to \Some\Really\Deep\Path\Somewhere\Else\On\The\Disk\Entirely\GroupB

然后将GroupA拖到项目树中的GroupB下,就像这样...

And you drag GroupA so it's under GroupB in your project tree tree, like so...

Project
   GroupB
       GroupA

由于GroupA指向磁盘上的物理文件夹(再次,请记住,这与组名和目录名是否匹配无关,仅与组指向实际目录无关),磁盘目录和 其所有内容是否被引用 实际上会物理移动到

Since GroupA points to a physical folder on-disk (again, remember, this has nothing to do with whether the group name and directory name match, only that the group points to an actual directory), the on-disk directory and all of its contents whether referenced or not will actually physically move to

\Some\Really\Deep\Path\Somewhere\Else\On\The\Disk\Entirely\GroupB\GroupA

再次,您将其移动到任何位置目标组的 actual 磁盘上的路径是。

Again, you are moving it to be under whatever the target group's actual on-disk path is.

现在,如果GroupB实际上没有指向文件夹,但是GroupA指向了文件夹,则最终位置解析到GroupB解析到的位置k,这意味着它将考虑所有其父组来确定位置。

Now, if GroupB doesn't actually point to a folder, but GroupA does, then the final location resolves to where GroupB resolves to on disk, which means it considers all of its parent groups to determine the location.

好消息是,对于95%的所有用例-确实是这样,除非您正在处理旧版代码库或正在积极更改新项目的结构,否则物理磁盘上的文件夹结构 会与项目结构匹配,并且所有这些文件夹引用应设置为相对于组,这意味着组实际上是在镜像文件系统。 这就是为什么人们认为从Xcode 9开始,您正在修改文件系统,但实际上并没有。您仍然只是在修改组和文件引用。 仅仅是Xcode做出的假设是,如果您的组结构镜像了物理磁盘,并且您对其进行了重命名/重新排序,则可能您还希望该物理磁盘更新,为您做到。

The good news is that for 95% of all use-cases--really, unless you are dealing with a legacy code-base or you actively alter a new project's structure--the physical on-disk folder structure will match the project structure, and all those folder references should be set 'Relative to Group' meaning the groups are essentially mirroring the file system. That is why people think starting with Xcode 9, you are modifying the file system, but again, you are not. You're still just modifying groups and file references. It's just Xcode is making assumptions that if your groups structure mirrors the physical disk and you rename/reorder them, chances are you also want the physical disk to update, so it does it for you. It's very convenient if albeit a little misleading.

有了这些新知识,如果事情变得不合时宜,并且无法按预期运行,则可能是时候检查一下了。您的文件和组引用以及相对位置。确保文件夹引用实际上指向您期望的位置,尤其是在升级较旧的项目时,并且您应该能够很快恢复正常。

With this new knowledge, if things get out of whack and aren't working as you're expecting them to, it may be time to check your file and group references and relative locations. Make sure your folder references are actually pointing to where you are expecting them to be, especially when upgrading older projects, and you should be able to get back on track pretty quickly.

无论如何,希望这更好地解释了事情。当然可以帮助我们! :)

Anyway, hope this better explains things. Sure helped us! :)

这篇关于Xcode组和文件夹引用之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆