xcode URLForResource 返回 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27999081/
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
URLForResource returns nil
提问by Fuzzy
I'm in the process of learning how to use xcode and how to write applications for the Mac.
我正在学习如何使用 xcode 以及如何为 Mac 编写应用程序。
So, I have a simple screensaver that loads an external file which works fine. I decided that it would be better to embed the html source in the application to reduce the external dependancies.
所以,我有一个简单的屏幕保护程序,可以加载一个工作正常的外部文件。我决定最好在应用程序中嵌入 html 源代码以减少外部依赖。
I dragged the HTML file to the resources folder, it's at the top level there's no sub folder or anything.
我将 HTML 文件拖到资源文件夹中,它在顶层,没有子文件夹或任何东西。
I made sure the Add to targets had my application in it, along with selecting 'create folder references' and 'copy files if needed'. I use the following to get the url to the file, but it returns nil
我确保添加到目标中有我的应用程序,同时选择“创建文件夹引用”和“如果需要复制文件”。我使用以下内容获取文件的 url,但它返回 nil
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"embedded" withExtension:@"html"];
System Preferences[10185]: fileUrl returned (null)
I've read through all the other SO issues for the similar problems but none of the fixes seem to apply. I've gone through and checked them.
我已经阅读了所有其他类似问题的 SO 问题,但似乎没有任何修复程序适用。我已经通过并检查了它们。
If I show the package contents of the final build the embedded.html is in there. Am I missing something really obvious?
如果我显示最终构建的包内容,embedded.html 就在那里。我错过了一些非常明显的东西吗?
edit to add
编辑添加
In build phases it's listed in Copy Bundle Resources
在构建阶段,它在 Copy Bundle Resources 中列出
xcode version is 6.1.1
xcode 版本是 6.1.1
回答by Fuzzy
After some considerable digging and a smidgeon of luck I found that
经过一些相当大的挖掘和一点点运气,我发现
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"embedded" withExtension:@"html"];
is incorrect and I should be using
不正确,我应该使用
NSURL *fileUrl = [[NSBundle bundleForClass:[self class]] URLForResource:@"embedded" withExtension:@"html"];
instead.
反而。
i.e. NSBundle bundleForClass:[self class]and not NSBundle mainBundle will give access to a screensaver bundle.
即 NSBundle bundleForClass:[self class]而不是 NSBundle mainBundle 将允许访问屏幕保护程序包。
Might be obvious, but only if you know ;)
可能很明显,但前提是你知道;)
回答by Peter Brockmann
回答by olynoise
Quitting and restarting Xcode fixed this for me.
退出并重新启动 Xcode 为我解决了这个问题。
回答by quarac
using this solved my problem [myBundle URLForResource: withExtension: subdirectory: ]
使用这个解决了我的问题 [myBundle URLForResource: withExtension: subdirectory: ]
Subdirectory queries to NSBundle URLForResource need to specify the subdirectory. Otherwise it returns nil.
对 NSBundle URLForResource 的子目录查询需要指定子目录。否则返回零。
One hour figuring this today :'(
今天花了一个小时来计算:'(
回答by Song Bi
I met a same issue in a MAC application. After several hours, I found an easy way to fix it.
我在 MAC 应用程序中遇到了同样的问题。几个小时后,我找到了一种简单的方法来修复它。
1, Make sure there is a phase named "Copy Files" under "Build Phases". If not, please add one.
1、确保在“Build Phases”下有一个名为“Copy Files”的阶段。如果没有,请添加一个。
2, Add your resources into the phase.
3, Rebuild your project and you can find your resources located in ~/Library/Developer/Xcode/DerivedData//Build/Products/Debug/.
3,重建你的项目,你可以在~/Library/Developer/Xcode/DerivedData//Build/Products/Debug/找到你的资源。