xcode 将子目录中的资源文件复制到应用程序包的根目录

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

Resource files in subdirectory are copied to root directory of the app bundle

xcoderesourcesdirectory

提问by neevek

I am not sure whether this is expected or it is bug. I am trying to create the same folder structure in disk as that in XCode. I use the following steps to add resource files to my project:

我不确定这是预期的还是错误的。我正在尝试在磁盘中创建与 XCode 中相同的文件夹结构。我使用以下步骤将资源文件添加到我的项目中:

  1. Create a Resourcesfolder under the root folder of the project.
  2. Drag the Resourcesfolder to XCode, which creates a Groupfor the folder.
  3. Drag my resource files from Finderto the Resourcesfolder in XCode.
  1. Resources在项目的根文件夹下创建一个文件夹。
  2. Resources文件夹拖到 XCode,它会Group为文件夹创建一个。
  3. 将我的资源文件从XCode 中FinderResources文件夹拖到该文件夹中。

And now I can see all the resource files are listed in Build Phrases->Copy Bundle Resources.

现在我可以看到所有资源文件都列在Build Phrases-> 中Copy Bundle Resources

The weird thing is that [[NSBundle mainBundle] pathForResource:@"book" ofType:@"epub"]returns a file path that can be located, while [[NSBundle mainBundle] pathForResource:@"book" ofType:@"epub" inDirectory:@"Resources"]returns (null).

奇怪的是[[NSBundle mainBundle] pathForResource:@"book" ofType:@"epub"]返回一个可以定位的文件路径,而[[NSBundle mainBundle] pathForResource:@"book" ofType:@"epub" inDirectory:@"Resources"]返回(null).

From the Build Resultswindow, I notice this line:

Build Results窗口,我注意到这一行:

CpResource MBookReader/Resources/book.epub /Users/neevek/Library/Developer/Xcode/DerivedData/workspace-fykuaikryknuhxgevcdtspkspptj/Build/Products/Debug-iphonesimulator/MBookReader.app/book.epub
cd /Users/neevek/workspace/xcode_projects/MBookReader
setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
builtin-copy -exclude .DS_Store -exclude CVS -exclude .svn -exclude .git -exclude .hg -resolve-src-symlinks -strip-tool /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip /Users/neevek/workspace/xcode_projects/MBookReader/MBookReader/Resources/book.epub /Users/neevek/Library/Developer/Xcode/DerivedData/workspace-fykuaikryknuhxgevcdtspkspptj/Build/Products/Debug-iphonesimulator/MBookReader.app

which shows that Resources/book.epubfile is indeed copied to MBookReader.app/(the root directory of the app bundle).

这表明该Resources/book.epub文件确实已复制到MBookReader.app/(应用程序包的根目录)。

Now Resourcesis a real folder in disk, why the folder itself is not copied?

现在Resources是磁盘中的真实文件夹,为什么不复制文件夹本身?

PS. I am using XCode 4.3.2.

附注。我正在使用 XCode 4.3.2。

回答by Alexei Sholik

When adding your Resources folder to Xcode, choose "Create folder references" instead of "Create groups for any added folders" in the sliding window.

将您的资源文件夹添加到 Xcode 时,请在滑动窗口中选择“创建文件夹引用”而不是“为任何添加的文件夹创建组”。

That is, replace the 3 steps in your question with the following ones.

也就是说,将问题中的 3 个步骤替换为以下步骤。

  1. Create a Resourcesdirectory under the root directory of the project. Organize your directory structure inside the Resourcesdirectory the way you want it to be.
  2. Drag the Resourcesdirectory to XCode and select the Create folder referencesoption.
  1. 在项目的根目录下创建一个Resources目录。按照您希望的方式在Resources目录中组织您的目录结构。
  2. 资源目录拖到 XCode 并选择创建文件夹引用选项。

You're done. Xcode will copy the contents of your Resourcesdirectory recursively into the bundle.

你完成了。Xcode 将递归地将您的Resources目录的内容复制到包中。

You might have trouble further down the road when Xcode doesn't copy a modified file somewhere inside the Resourcesdirectory. A clean build usually fixes this. Alternatively, you can remove the .app file before building. This will not cause unmodified source files to rebuild, but will force Xcode to copy all of the resources anew.

当 Xcode 没有将修改后的文件复制到Resources目录中的某个位置时,您可能会遇到麻烦。一个干净的构建通常可以解决这个问题。或者,您可以在构建之前删除 .app 文件。这不会导致未修改的源文件重建,但会强制 Xcode 重新复制所有资源。

Sample Folder Reference

示例文件夹参考