我应该如何组织我的 Xcode 项目文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3590380/
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
How should I organize my Xcode project files?
提问by Justin808
I'm trying to wrap my head around Xcode's file organization - or lack there of. I can do all I want in project and it looks great with all the "fake" folders and structure. I go look at the file system and boom HUGEmess. I've tried importing files with the Create Folder Reference for any added folder
option checked and that works, kinda. I get the structure I want both in Xcode and on the filesystem.
我试图围绕 Xcode 的文件组织进行思考 - 或者缺乏。我可以在项目中做我想做的所有事情,并且所有“假”文件夹和结构看起来都很棒。我去看看文件系统和繁荣巨大的混乱。我试过在Create Folder Reference for any added folder
选中该选项的情况下导入文件,这很有效,有点。我在 Xcode 和文件系统中都得到了我想要的结构。
Issues: When I add a file to a folder on the filesystem that is a Folder Reference in Xcode, its not in Xcode when I go look, not even after reloading the project. Files/Subfolders in a Folder Reference can't be moved around in Xcode. When I move them on the filesystem I get red links (can't find the file?) in Xcode.
问题:当我将一个文件添加到文件系统上作为 Xcode 文件夹引用的文件夹时,当我查看时它不在 Xcode 中,即使在重新加载项目后也不在。文件夹引用中的文件/子文件夹不能在 Xcode 中移动。当我在文件系统上移动它们时,我在 Xcode 中得到红色链接(找不到文件?)。
How do I keep a organized project andfilesystem? How can I set up a project to just recognize a folder and show its (current and up-to-date) files and subfolders in my project?
我如何保持一个有组织的项目和文件系统?如何设置项目以仅识别文件夹并在我的项目中显示其(当前和最新)文件和子文件夹?
Another issue I seem to run into, if I use a Folder Reference and change a file, the file is not updated in my application unless I do a full clean & rebuild. If I don't use a Folder Reference, allmy files are dumped into the Resource folder of the application bundle, not in the nice structure I have in my project.
我似乎遇到的另一个问题,如果我使用文件夹引用并更改文件,除非我进行完全清理和重建,否则我的应用程序中不会更新该文件。如果我不使用文件夹引用,我的所有文件都会转储到应用程序包的 Resource 文件夹中,而不是在我的项目中的良好结构中。
Should I care at all? Should I just use the fake folders and let everything go everywhere and not care? My application bundle will be a mess, the filesystem will be a mess, but it will all work... I would hope?
我应该关心吗?我应该只使用假文件夹并让所有东西随处可见而不在乎吗?我的应用程序包会一团糟,文件系统也会一团糟,但一切都会好起来的……我希望?
Edit:
编辑:
My biggest reason for wanting an organized filesystem is that the resource files (images, sounds, other datafiles, etc.) are not edited in Xcode. I have to access them in 3rd party apps via the filesystem. If its a mess things are harder to find and maintain in the other 3rd party applications.
我想要一个有组织的文件系统的最大原因是资源文件(图像、声音、其他数据文件等)不在 Xcode 中编辑。我必须通过文件系统在 3rd 方应用程序中访问它们。如果它一团糟,则在其他 3rd 方应用程序中更难找到和维护。
Also what happens if I want a structure like the following:
如果我想要如下结构,会发生什么:
- Images/Backgrounds/Name.png
- Images/Icons/Name.png
- Images/Titles/Name.png
- 图像/背景/名称.png
- 图像/图标/名称.png
- 图像/标题/名称.png
Should I use long filenames rather than folders to organize?
我应该使用长文件名而不是文件夹来组织吗?
- Images_Backgrounds_Name.png
- Images_Icons_Name.png
- Images_Titles_Name.png
- Images_Backgrounds_Name.png
- Images_Icons_Name.png
- Images_Titles_Name.png
采纳答案by Thomas Clayson
Should I care at all? Should I just use the fake folders and let everything go everywhere and not care? My application bundle will be a mess, the filesystem will be a mess, but it will all work... I would hope?
我应该关心吗?我应该只使用假文件夹并让所有东西随处可见而不在乎吗?我的应用程序包会一团糟,文件系统也会一团糟,但一切都会好起来的……我希望?
IMO no... :) basically. The whole point is that XCode has been designed to give you the best experience of programming. If Apple wanted you to physically organise all your files and folders within the actual filesystem then they would have made it that way.
IMO 没有... :) 基本上。重点是 XCode 旨在为您提供最佳的编程体验。如果 Apple 希望您在实际文件系统中物理组织所有文件和文件夹,那么他们就会这样做。
I don't really understand why you would want to organise all the files and folders in this way anyway? It makes no difference to the running of the application and the "fake" folders (groups) in XCode adequately provide the necessary visual aid for yourself (and others) to navigate through your classes and other resources. Organising it correctly in your filesystem (as you have found) surely just makes things more difficult?
我真的不明白你为什么要以这种方式组织所有文件和文件夹?它对应用程序的运行没有影响,并且 XCode 中的“假”文件夹(组)足以为您自己(和其他人)提供必要的视觉帮助,以浏览您的类和其他资源。在您的文件系统中正确组织它(如您所见)肯定只会让事情变得更加困难?
回答by ma11hew28
I also wish Xcode automatically kept itself and the file system in sync.
我还希望 Xcode 自动保持自身和文件系统同步。
So much so, that I spent an hour doing so manually for a project called acani-iphone on GitHub. Basically, I just moved some of the files around using Finder, creating new folders as I pleased. Then, I switched back to Xcode and saw that the files I just moved were now red (because Xcode was thinking they're where I moved them from and so couldn't find them).
如此之多,以至于我花了一个小时在 GitHub 上为一个名为acani-iphone的项目手动执行此操作。基本上,我只是使用 Finder 移动了一些文件,随心所欲地创建了新文件夹。然后,我切换回 Xcode,看到我刚刚移动的文件现在是红色的(因为 Xcode 认为它们是我移动它们的位置,因此找不到它们)。
UPDATE: I just figured out that I could've then just clicked on the red group or file, pressed CMD+i (Get Info from the context menu, which you can open by right-clicking on the red file or group), and under the General tab, clicked Choose, then found where I moved the file to in the filesystem. But, I didn't do that, here's what I did instead, which also works:
更新:我刚刚发现我可以点击红色的组或文件,按下 CMD+i(从上下文菜单中获取信息,您可以通过右键单击红色的文件或组打开),然后在“常规”选项卡下,单击“选择”,然后在文件系统中找到我将文件移动到的位置。但是,我没有这样做,这是我所做的,这也有效:
Then, I just highlighted all the red files in Xcode and pressed command + delete
to delete the broken (red) references. Then, I right-clicked on the Group I wanted to add the files to (usually the same group), and clicked Add
> Existing Files...
. Then, I found the same files in the new spot on the file system. I kept "Copy items into destination group's folder (if needed)" unchecked, I checked the radio button "Recursively create groups for any added folders," and I checked add to target acani if the files I was adding were being used to build the acani iPhone app.
然后,我只是突出显示了 Xcode 中的所有红色文件,然后按下command + delete
以删除损坏的(红色)引用。然后,我右键单击要添加文件的组(通常是同一个组),然后单击Add
> Existing Files...
。然后,我在文件系统的新位置找到了相同的文件。我保持“将项目复制到目标组的文件夹(如果需要)”未选中,我选中了单选按钮“为任何添加的文件夹递归创建组”,如果我添加的文件被用于构建阿卡尼 iPhone 应用程序。
I did the above with like a directory of files at a time. A few times I was more aggressive, adding multiple directories at a time, since I almost always selected the radio button "Recursively create groups for any added folders."
我一次用一个文件目录完成了上面的操作。有几次我更激进,一次添加多个目录,因为我几乎总是选择单选按钮“为任何添加的文件夹递归创建组”。
I found out that the files acani_Prefix.pch
and acani-Info.plist
had to stay in the root file system dir (although there may be settings you can set to allow these files to be elsewhere, like I think you can add a line to acani-Info.plist
so that you can move/rename acani_Prefix.pch
, but I'm fine with them in the root dir on the file system.
我发现的文件acani_Prefix.pch
和acani-Info.plist
必须留在根文件系统目录(虽然有可能设置,您可以设置为允许这些文件在其他地方,就像我认为你可以添加一条线,acani-Info.plist
这样就可以移动/重命名acani_Prefix.pch
,但是我在文件系统的根目录中对它们很好。
That was annoying to do, and perhaps not even worth the trouble, perhaps procrastination, but going forward, before adding existing files to Xcode, I'll first make sure they're in the place I want them to be on the file system.
这样做很烦人,甚至可能不值得麻烦,也许是拖延,但是继续前进,在将现有文件添加到 Xcode 之前,我将首先确保它们位于我希望它们在文件系统上的位置。
回答by pro7otype
OK, so here is how it works:
好的,这是它的工作原理:
Xcode doesn't know about any files until you tell it about them. That is, even if you add a file manually in the finder (usually a bad idea) to a folder that contains files in an Xcode project, it doesn't know about them until you "add existing file to project".
Xcode 不知道任何文件,直到你告诉它。也就是说,即使您在 finder 中手动添加一个文件(通常是一个坏主意)到包含 Xcode 项目中文件的文件夹,它也不知道它们,直到您“将现有文件添加到项目”。
The best practice (imo) for adding an existing file (or group of files) to a project (say, some code you just downloaded) is to choose "add existing files" and then "copy items to destination group's folder (if needed)" in the next dialog, if you want your project to have a copy of the files in question, rather than merely a reference to them (there are advantages and disadvantages of both).
将现有文件(或文件组)添加到项目(例如,您刚刚下载的某些代码)的最佳实践 (imo) 是选择“添加现有文件”,然后选择“将项目复制到目标组的文件夹(如果需要) " 在下一个对话框中,如果您希望您的项目拥有相关文件的副本,而不仅仅是对它们的引用(两者都有优点和缺点)。
Don't worry too much about the naming of folders in Xcode, or where you put things, but try to keep to a standard that makes sense in your environment. For example, I always put the classes I write in "Classes", and have separate folders for any library code i've downloaded for use in the project. I always put images/icons/audio etc in to "Resources".
不要太担心 Xcode 中文件夹的命名,或者你把东西放在哪里,但尽量保持在你的环境中有意义的标准。例如,我总是将我编写的类放在“类”中,并为我下载以在项目中使用的任何库代码设置单独的文件夹。我总是把图像/图标/音频等放在“资源”中。
In short, if you like what's in the project folder to be approximately the same as what's in your project, always add existing files by choosing the "copy items to destination group's folder"
简而言之,如果您希望项目文件夹中的内容与项目中的内容大致相同,请始终通过选择“将项目复制到目标组文件夹”来添加现有文件
The flexibility in XCode is intentional. It's up to you to decide how you like to organise things.
XCode 的灵活性是有意的。由您决定如何组织事物。
回答by joerick
回答by Graham Perks
It would be great if Xcode could keep itself and the file system in sync. Unfortunately it doesn't. One reason for wanting it to is so the hierarchy in your SCCS matches the one in Xcode.
如果 Xcode 能够保持自身和文件系统同步,那就太好了。不幸的是它没有。想要它的原因之一是 SCCS 中的层次结构与 Xcode 中的层次结构相匹配。
I fall back to keeping things organized in Xcode, and leaving the file system separated into not much more than "Classes" and "Resources".
我退回到在 Xcode 中组织事物,并将文件系统分为“类”和“资源”。
回答by Kal
This changed with Xcode 9. From the release notes:
这在 Xcode 9 中发生了变化。来自发行说明:
Groups in the Project Navigator are now more closely associated with directories in the file system. (28612132)
- Dragging files between groups in the Project Navigator moves the files in the filesystem and updates any associated SCM working copies.
- When a group is connected to folder in the filesystem, creating, renaming, and deleting groups updates the corresponding files and folders in the the filesystem.
- To remove a connection between a group and a folder in the filesystem, select the group, and then open the File inspector and click on the on the Clear path button (X).
- To add or update an association from a file or a folder in the filesystem to a file or a group in the project, select the file or group, open the File inspector, and drag the corresponding file or folder onto the Location section in the File inspector.
项目导航器中的组现在与文件系统中的目录更密切相关。(28612132)
- 在项目导航器中的组之间拖动文件会移动文件系统中的文件并更新任何关联的 SCM 工作副本。
- 当一个组连接到文件系统中的文件夹时,创建、重命名和删除组会更新文件系统中的相应文件和文件夹。
- 要删除文件系统中组和文件夹之间的连接,请选择组,然后打开文件检查器并单击清除路径按钮 (X)。
- 要添加或更新文件系统中的文件或文件夹与项目中的文件或组的关联,请选择文件或组,打开文件检查器,然后将相应的文件或文件夹拖到文件中的位置部分检查员。
The new behaviour is available from the 'New Group with Folder' command (which may appear as just 'New Group'), while the old behaviour is available from the 'New Group withoutFolder' command (which may also appear as just 'New Group'!) The dominant usage amongst any existing groups in the target folder seems to determine which command gets labelled 'New Group'. It's more than a little confusing, but if you are in the habit of choosing one or the other, the idea seems to be that you can just stick with the default 'New Group' command. (See rob mayoff's far more thorough explanation.)
新行为可从“带文件夹的新组”命令(可能仅显示为“新组”)中获得,而旧行为可从“无文件夹的新组”命令中获得(也可能仅显示为“新组”)Group'!) 目标文件夹中任何现有组的主要用法似乎决定了哪个命令被标记为“新组”。这有点令人困惑,但是如果您习惯于选择其中之一,那么您的想法似乎是您可以坚持使用默认的“新建组”命令。(参见rob mayoff 更彻底的解释。)
回答by levous
What I do is create a group to represent each folder and then, before adding files to it, in the right panel, first tab, immediately below "Path", there is an icon that allows you to choose the folder. In that folder dialog, I create a folder that matches the group and choose it.
我所做的是创建一个组来代表每个文件夹,然后在向其中添加文件之前,在右侧面板的第一个选项卡中,紧邻“路径”下方,有一个图标可让您选择文件夹。在该文件夹对话框中,我创建了一个与该组匹配的文件夹并选择它。
In xcode3, this resulted in new and add files dialogs starting in this path. That made it worth the effort. Xcode4, however, does not respect this setting. Therefore, its questionable whether there is any real value in it. I also wish XCOde would support better file system organization.
在 xcode3 中,这会导致在此路径中开始新建和添加文件对话框。这使得它值得付出努力。但是,Xcode4 不遵守此设置。因此,它是否有任何实际价值值得怀疑。我也希望 XCOde 能够支持更好的文件系统组织。
Considering that file names must be unique within a project, regardless of groups and folders, there is justification for accepting the flat folder structure default and using groups for IDE convenience. Its difficult to come from other platforms where this is frowned upon.
考虑到文件名在项目中必须是唯一的,而不管组和文件夹如何,有理由接受平面文件夹结构默认值并使用组来方便 IDE。很难来自其他不受欢迎的平台。
回答by Nir Pengas
i feel you and personally cannot NOT care about the actual structure and just rely on workspaces.
我觉得你和个人不能不关心实际结构而只依赖工作区。
what would be really great is a tool that will go over the workspace structure and re-organize the file system accordingly, taking care of any re-naming of folders etc. this would be a classic solution and IMHO should be implemented as an option as we re-organize our project as we move about it.
真正伟大的是一个工具,它将检查工作区结构并相应地重新组织文件系统,处理文件夹的任何重命名等。这将是一个经典的解决方案,恕我直言应该作为一个选项实施随着我们的行动,我们重新组织了我们的项目。
some issues could be source control though xcode4 works with both git and SVN.
尽管 xcode4 适用于 git 和 SVN,但有些问题可能是源代码控制。