Xcode 项目资源组织和结构

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

Xcode Project Resource Organization and Structure

xcoderesourcesinterface-builder

提问by Jorge Israel Pe?a

I am relatively new to Xcode and one thing that has bothered me is that when I add a resource it gets added to the top level directory of my project directory. So for example, at the moment, all of my images are at the top level directory and it makes things look messy. I rather would've liked, for example, to have an images/folder and then sub-folders within that to store images for certain things, like tab-bar/icons. Is this possible? Or is it convention to just keep everything in the top level directory?

我对 Xcode 比较陌生,让我困扰的一件事是,当我添加资源时,它会被添加到我的项目目录的顶级目录中。例如,目前,我的所有图像都位于顶级目录中,这让事情看起来很混乱。例如,我宁愿拥有一个图像/文件夹,然后在其中设置子文件夹来存储某些事物的图像,例如标签栏/图标。这可能吗?还是将所有内容都保留在顶级目录中是惯例?

Someone in the #iphonedevchannel at freenode told me something about the build process rule or something, it just seemed too complicated I figured I shouldn't bother, and that most people just kept things at their root directory considering the complexity of the solution. Perhaps it isn't all that complicated though, I just felt it should be easier.

有人在#iphonedev在freenode的渠道告诉我一些关于构建过程的规则或东西,它只是似乎太复杂,我想我不应该去,而大多数人只是在自己的根目录下把每一件事情考虑解决方案的复杂性。也许这并不那么复杂,我只是觉得它应该更容易。

For example I would also like to have a sub-folder for my views, like views/that stores all of my nib files; currently they are all in the top level directory. And the same goes for my property lists.

例如,我还想为我的视图创建一个子文件夹,比如存储我所有 nib 文件的views/;目前它们都在顶级目录中。我的财产清单也是如此。

I would appreciate it if you could tell me what the convention is, I doubt it is to just store everything in the top level directory. And I'm referring to the actual file system, not simply creating 'virtual folders' in Xcode to organize things, because the top level directory of my project will still be a mess. And if it is possible to organize things better, I would appreciate any detailed help on how to accomplish it.

如果您能告诉我约定是什么,我将不胜感激,我怀疑是否将所有内容都存储在顶级目录中。我指的是实际的文件系统,而不是简单地在 Xcode 中创建“虚拟文件夹”来组织内容,因为我的项目的顶级目录仍然是一团糟。如果可以更好地组织事情,我将不胜感激任何有关如何完成它的详细帮助。

I think I have heard that in the final build, the entire file structure gets squashed to the root directory. I don't mind so much about that, I just want things to be organized while I am working on my project. I think this is where that 'build rule' thing comes in, but I'm not sure how or what to do.

我想我听说在最终构建中,整个文件结构被压缩到根目录。我对此并不介意,我只是希望在我进行项目时能够将事情组织起来。我认为这就是“构建规则”的用武之地,但我不确定该怎么做或该怎么做。

采纳答案by Rob Napier

Here's a little more explanation of Graham's suggestion:

以下是对格雷厄姆建议的更多解释:

  • Create an images directory
  • Create an images Group in Xcode
  • Right-click the group and Get Info
  • "Choose..." to set the group's directory to be your images directory
  • 创建图像目录
  • 在 Xcode 中创建一个图像组
  • 右键单击组并获取信息
  • “选择...”将组的目录设置为您的图像目录

Now, anything you put in the images group will automatically go into the images directory. That's true if you drag/drop from within Xcode, from other other projects or from Finder, or if you Add New... by right-clicking on the images directory. (This assumes you leave the default setting of "relative to group" in the info pane.)

现在,您放入图像组的任何内容都将自动进入图像目录。如果您从 Xcode、其他项目或 Finder 中拖/放,或者如果您通过右键单击图像目录来添加新... (这假设您在信息窗格中保留“相对于组”的默认设置。)

This will work for anything you put in that group (NIBs, etc).

这适用于您放入该组的任何内容(NIB 等)。

For imageNamed:, you just give the base name.

对于imageNamed:,您只需提供基本名称。

My recommendation is to keep a small number of filesystem directories, and mostly organize with groups in Xcode. But I agree, a small number of filesystem directories are very helpful. I put all my source code in a Classes directory (just because Apple used that name; I would have picked "Sources" or something). I also have a Resources directory at the top level and generally create images and audio directories under that. But I don't generally create a lots of real directories beyond that. This keeps things a bit tidier, but makes it easier to grep and wildcard things with the shell.

我的建议是保留少量文件系统目录,并且主要在 Xcode 中使用组进行组织。但我同意,少量的文件系统目录非常有用。我将所有源代码放在 Classes 目录中(只是因为 Apple 使用了这个名称;我会选择“Sources”或其他名称)。我在顶层也有一个 Resources 目录,通常在该目录下创建图像和音频目录。但除此之外,我通常不会创建很多真正的目录。这让事情变得更整洁,但更容易使用 shell 进行 grep 和通配符。

Beyond shell tools, the big advantage of organizing by groups rather than directories is the impact on version control systems like subversion. When you suddenly realize that your "Views" group is way too big, you can reorganize it without taking the huge impact of moving files around.

除了 shell 工具之外,按组而不是目录组织的一大优势是对版本控制系统(如 subversion)的影响。当您突然意识到您的“视图”组太大时,您可以重新组织它,而无需考虑移动文件的巨大影响。

回答by nont

The accepted answer didn't work for me in Xcode 4.3.2. Looks like they removed the "Get info" menu option. Instead I did this:

接受的答案在 Xcode 4.3.2 中对我不起作用。看起来他们删除了“获取信息”菜单选项。相反,我这样做了:

  1. Created the directory on disk where I wanted it.

  2. Went to the file menu

  3. Right clicked on the group I wanted to add to

  4. Selected "Add Files to PROJECTNAME"

  5. Added the files from the directory I created in (1)

  1. 在我想要的磁盘上创建目录。

  2. 转到文件菜单

  3. 右键单击我想添加到的组

  4. 选择“将文件添加到 PROJECTNAME”

  5. 添加了我在(1)中创建的目录中的文件

回答by IlDan

Life is hard for IDEs. They have to mediate between a filesystem world, where the compiler, with its source files live, and the UI world, where programmers work.

IDE 的生活很艰难。他们必须在文件系统世界(编译器及其源文件所在的位置)和UI 世界(程序员工作的地方)之间进行调解。

In the old times that formed the contemporary coding culture, everybody, compilers and people, used to live all togheter in the filesystem. So today we still have a tendency to think we want to control where sources and resources reside on the disk. That is good, because we may need to know exactly what compilers eat.

在形成当代编码文化的旧时代,每个人,编译器和人,过去都生活在文件系统中。所以今天我们仍然倾向于认为我们想要控制源和资源在磁盘上的位置。这很好,因为我们可能需要确切地知道编译器吃什么。

Anyway if you think your IDE just as a the frontend of a funny database system of source code and if you really have full confidence about its capabilities and features (which is seldom the case), you may get to the conclusion that if you just don't care about the filesystem you may have a better life, as a programmer (and as a human being).

无论如何,如果您认为您的 IDE 只是一个有趣的源代码数据库系统的前端,并且您真的对其功能和特性充满信心(这种情况很少发生),那么您可能会得出结论,如果您只是不这样做不关心文件系统,作为程序员(和人类),您可能会过上更好的生活。

You're right wondering, because Xcode is not very good nor friendly about filesystem code organization and people that is (correctly) picky on that will learn how to move files on their rightfolder on disk, and on their rightXcode group. That, if you use the "Create folder references for any added folders", have a 1:1 relation.

你想知道是对的,因为 Xcode 对文件系统代码组织不是很好也不友好,而且那些(正确地)挑剔的人将学习如何在磁盘上正确的文件夹和正确的Xcode 组中移动文件。也就是说,如果您使用“为任何添加的文件夹创建文件夹引用”,则具有 1:1 的关系。

But you will see many people, and many project examples from Apple itsef, that use a simple flat filesystem tree with everything in the same folder.

但是你会看到很多人,以及来自 Apple itef 的许多项目示例,它们使用一个简单的平面文件系统树,所有内容都在同一个文件夹中。

My advice is that you at least group public headers and resources in different folders, something à la Java/Maven, but there is no universal convention.

我的建议是您至少将公共头文件和资源分组在不同的文件夹中,类似于 Java/Maven,但没有通用约定。

回答by IlDan

It's actually really easy to get from where you are to where you want to be. Create the images/folder and put all the images in it. Now in the project window in Xcode, the image files have gone red because they aren't in place any more. Get Info on them, and "Choose..." the new location.

从你所在的地方到你想去的地方实际上真的很容易。创建images/文件夹并将所有图像放入其中。现在在 Xcode 的项目窗口中,图像文件变红了,因为它们不再存在。获取有关它们的信息,然后“选择...”新位置。

Note that just because you've moved the file in the source file system (known as $SRCROOT), there's no change to the way the file is used in the product. If it got copied into Contents/Resources/before you moved the file, that's where it gets copied to afterwards. No subfolder will be added at the destination.

请注意,仅仅因为您已经在源文件系统(称为$SRCROOT)中移动了文件,文件在产品中的使用方式并没有改变。如果它Contents/Resources/在您移动文件之前被复制到,那么它就是之后被复制到的地方。不会在目的地添加子文件夹。