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

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

问题描述

我不确定这是预期的还是错误的.我正在尝试在磁盘中创建与 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. 在项目的根文件夹下创建一个Resources文件夹.
  2. Resources 文件夹拖到 XCode,这会为该文件夹创建一个 Group.
  3. 将我的资源文件从 Finder 拖到 XCode 中的 Resources 文件夹.
  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.

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

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

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

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

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

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

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

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

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

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

附注.我使用的是 XCode 4.3.2.

PS. I am using XCode 4.3.2.

推荐答案

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

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

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

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

  1. 在项目根目录下创建一个Resources目录.按照您希望的方式在 Resources 目录中组织您的目录结构.
  2. Resources 目录拖到 XCode 并选择创建文件夹引用选项.
  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.

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

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

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

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天全站免登陆