xcode iOS项目目录组织
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8871917/
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
iOS project directory organisation
提问by allaire
Just a quick question/opinion about how you arrange Xcode 4 directory for each of your iOS app you create.
只是关于您如何为您创建的每个 iOS 应用程序安排 Xcode 4 目录的快速问题/意见。
Personally, I still add a directory called /Resources which contains sub-directories like /Fonts, /Images, /Icons, etc. I use the option "Copy items to destination group's folder (if needed)". If I need to add a new ressource, I simply add it to the right directory in finder.app in my project directory (i.e if it's an image, I add it to MyProject/Resources/Images/Foo.png) and then I simply go on my group "Images" in Xcode and add the file from there.
就个人而言,我仍然添加一个名为 /Resources 的目录,其中包含 /Fonts、/Images、/Icons 等子目录。我使用选项“将项目复制到目标组的文件夹(如果需要)”。如果我需要添加一个新的资源,我只需将它添加到我的项目目录中 finder.app 的正确目录中(即如果它是一个图像,我将它添加到 MyProject/Resources/Images/Foo.png)然后我简单地继续我在 Xcode 中的“图像”组并从那里添加文件。
I once tried to use "Folder references" option to keep Xcode and Finder in sync, but IB was not able to get the images...
我曾经尝试使用“文件夹引用”选项来保持 Xcode 和 Finder 同步,但是 IB 无法获取图像...
Also, is your Icon.png and Default.png at the root of your project? (i.e with the .xcodeproj file?) When you use Xcode 4 (in project info > summary) they add them there... Wondered if you keep them there, Cause personally I always prefer having all my images in a sample directory!
另外,你的 Icon.png 和 Default.png 是你项目的根目录吗?(即使用 .xcodeproj 文件?)当您使用 Xcode 4(在项目信息 > 摘要中)时,他们会将它们添加到那里...想知道您是否将它们保留在那里,因为就我个人而言,我总是更喜欢将所有图像放在示例目录中!
And what about photoshop files or any other files relative to your project, do you put them in the root directory too (again with the .xcodeproj file) ?
还有 photoshop 文件或与您的项目相关的任何其他文件,您是否也将它们放在根目录中(再次使用 .xcodeproj 文件)?
回答by Nick Lockwood
I suggest keeping all your .m and .h and .xib files in a single folder because it simplifies localizing strings when you run ibtool to extract all the NSLocalizedString references, and it also makes it easier to move files around int the virtual folder structure within Xcode when you refactor your project.
我建议将所有 .m、.h 和 .xib 文件保存在一个文件夹中,因为当您运行 ibtool 以提取所有 NSLocalizedString 引用时,它可以简化本地化字符串,并且还可以更轻松地在虚拟文件夹结构中移动文件重构项目时的 Xcode。
Keep third party libraries or frameworks in a separate folder to avoid mixing them up with your own code.
将第三方库或框架保存在单独的文件夹中,以避免将它们与您自己的代码混淆。
Within Xcode itself, I organise these class files into separate virtual folders called Views, Controllers, Model and Nibs (and Libraries for 3rd party stuff). If it's a large project I break it into folders for each component and then break those into views, controllers, etc, but again that's only virtual folders, not physical ones.
在 Xcode 本身中,我将这些类文件组织到名为 Views、Controllers、Model 和 Nibs(以及用于 3rd 方内容的库)的单独虚拟文件夹中。如果它是一个大型项目,我会将它分解为每个组件的文件夹,然后将它们分解为视图、控制器等,但这同样只是虚拟文件夹,而不是物理文件夹。
Keeping resources in a separate physical folder is a good idea just to make it easier to browse them on disk. It may make sense to split these into images, sounds, xml, etc. Within Xcode you may want to break these down into virtual subfolders by component or category (interface, content, etc) but I wouldn't suggest doing that on disk as it makes it harder to refactor your folder structure as your project grows.
将资源保存在单独的物理文件夹中是一个好主意,只是为了更容易在磁盘上浏览它们。将它们拆分为图像、声音、xml 等可能是有意义的。在 Xcode 中,您可能希望按组件或类别(界面、内容等)将它们分解为虚拟子文件夹,但我不建议在磁盘上这样做随着项目的增长,重构文件夹结构变得更加困难。
It's a really good idea to keep all your images in the same folder because it avoids you accidentally giving two images the same name. Xcode won't warn you if you import two images with the same name from different places, but when it builds the app you'll end up with just one or the other, chosen at random.
将所有图像保存在同一个文件夹中是一个非常好的主意,因为它可以避免您不小心将两个图像命名为相同的名称。如果你从不同的地方导入两个同名的图像,Xcode 不会警告你,但是当它构建应用程序时,你最终只会随机选择一个或另一个。
You don't have to keep Default.png and Icon.png in the root (or even call them Default.png and Icon.png) but sometimes Xcode gets confused if standard files aren't where it expects.
您不必将 Default.png 和 Icon.png 保留在根目录中(甚至称它们为 Default.png 和 Icon.png),但有时如果标准文件不在预期位置,Xcode 会感到困惑。
And yeah, whatever you do, don't try to use the blue folder references when you import images otherwise you can't access them from within your code with imageNamed: or within Interface Builder. Use the yellow virtual folder references instead. Basically blue folders get copied into the app bundle as actual folders when the app is built, whereas the yellow folders are ignored and their contents go directly into the root of the app bundle. To access content in blue folders, you'll need to include the blue folder names in the path when you load them, e.g.
是的,无论您做什么,在导入图像时都不要尝试使用蓝色文件夹引用,否则您将无法从带有 imageNamed: 的代码或 Interface Builder 中访问它们。请改用黄色虚拟文件夹引用。基本上,在构建应用程序时,蓝色文件夹会作为实际文件夹复制到应用程序包中,而黄色文件夹将被忽略,它们的内容直接进入应用程序包的根目录。要访问蓝色文件夹中的内容,您需要在加载它们时在路径中包含蓝色文件夹名称,例如
NSString *path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png" inDirectory:@"nameOfBlueFolderInXcode"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
回答by camdez
I've been thinking about this a lot lately; here's the structure I decided to go with:
我最近一直在思考这个问题;这是我决定采用的结构:
ProjectName
ProjectName/Assets
ProjectName/Assets/Images
ProjectName/Frameworks
ProjectName/Logic
ProjectName/Logic/Models
ProjectName/Presentation
ProjectName/Presentation/ViewControllers
ProjectName/Presentation/Views
I think this gives a good structure for growth and provides a reasonable home for most anything. Of course, add additional subfolders (e.g. Assets/Fonts
) as needed. For my full rationale for the structure check out this blog post.
我认为这为增长提供了良好的结构,并为大多数事物提供了合理的家园。当然,Assets/Fonts
根据需要添加其他子文件夹(例如)。有关结构的完整原理,请查看此博客文章。
I do keep things like Icon.png
and Default.png
at the root of the project as Xcode seems finicky about it, but otherwise things are neatly organized. I keep my corresponding .m
, .h
, and .xib
files together, but I split up the view controllers logically into subfolders by feature (e.g. Initial
, Settings
).
我确实将类似Icon.png
和Default.png
作为项目的根,因为 Xcode 似乎对它很挑剔,但除此之外,事情都井井有条。我将相应的.m
、.h
和.xib
文件放在一起,但我按功能(例如Initial
,Settings
)将视图控制器逻辑地拆分为子文件夹。