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
Resource files in subdirectory are copied to root directory of the app bundle
提问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 中相同的文件夹结构。我使用以下步骤将资源文件添加到我的项目中:
- Create a
Resources
folder under the root folder of the project. - Drag the
Resources
folder to XCode, which creates aGroup
for the folder. - Drag my resource files from
Finder
to theResources
folder in XCode.
Resources
在项目的根文件夹下创建一个文件夹。- 将
Resources
文件夹拖到 XCode,它会Group
为文件夹创建一个。 - 将我的资源文件从XCode 中
Finder
的Resources
文件夹拖到该文件夹中。
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 Results
window, 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.epub
file is indeed copied to MBookReader.app/
(the root directory of the app bundle).
这表明该Resources/book.epub
文件确实已复制到MBookReader.app/
(应用程序包的根目录)。
Now Resources
is 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 个步骤替换为以下步骤。
- Create a Resourcesdirectory under the root directory of the project. Organize your directory structure inside the Resourcesdirectory the way you want it to be.
- Drag the Resourcesdirectory to XCode and select the Create folder referencesoption.
- 在项目的根目录下创建一个Resources目录。按照您希望的方式在Resources目录中组织您的目录结构。
- 将资源目录拖到 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 重新复制所有资源。