子目录中的资源文件被复制到应用程序包的根目录 [英] Resource files in subdirectory are copied to root directory of the app bundle

查看:183
本文介绍了子目录中的资源文件被复制到应用程序包的根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 创建一个资源文件夹。

  2. 资源文件夹拖动到XCode,该文件创建一个
  3. 将资源文件从 Finder 拖到资源文件夹在XCode。

现在我可以看到所有资源文件列在构建短语 - > 复制捆绑资源



奇怪的是 [[NSBundle mainBundle] pathForResource:@bookofType:@epub] 返回可以找到的文件路径,而 [[ NSBundle mainBundle] pathForResource:@bookofType:@epubinDirectory:@Resources] 返回(null)。 >

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

  CpResource MBookReader / Resources / book.epub / Users / neeve k / 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/开发人员/工具链/ XcodeDefault.xctoolchain / usr / bin / strip /Users/neevek/workspace/xcode_projects/MBookReader/MBookReader/Resources/book.epub / Users / neevek / Library / Developer / Xcode / DerivedData / workspace-fykuaikryknuhxgevcdtspkspptj / Build /产品/调试iphonesimulator / MBookReader.app

其中显示资源/书.epub 文件确实复制到 MBookReader.app / (根目录为t他的应用程序包)。



现在资源是磁盘中的一个真正的文件夹,为什么文件夹本身不被复制?



PS。我正在使用XCode 4.3.2。

解决方案

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



也就是将您的问题中的3个步骤替换为以下内容。


  1. 在项目的根目录下创建一个资源目录。将资源目录中的目录结构按照您想要的方式进行整理。

  2. 资源目录拖到XCode,然后选择创建文件夹参考选项。

你完成了Xcode将向您的资源目录中的内容递归复制到该包中。



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




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:

  1. Create a Resources folder under the root folder of the project.
  2. Drag the Resources folder to XCode, which creates a Group for the folder.
  3. Drag my resource files from Finder to the Resources folder in XCode.

And now I can see all the resource files are listed in 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).

From the Build Results window, I notice this line:

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).

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

PS. I am using XCode 4.3.2.

解决方案

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

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

  1. Create a Resources directory under the root directory of the project. Organize your directory structure inside the Resources directory the way you want it to be.
  2. Drag the Resources directory to XCode and select the Create folder references option.

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

You might have trouble further down the road when Xcode doesn't copy a modified file somewhere inside the Resources directory. 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.

这篇关于子目录中的资源文件被复制到应用程序包的根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆