从NSBundle获取零路径 [英] Getting a nil path from NSBundle
问题描述
我在项目中创建了一个新文件夹,在其中复制了图像(称为"io.jpg").
我还检查了构建阶段->复制捆绑软件资源,并且文件在那里.
因此,我尝试获取此图像的路径:
I have created a new folder in my project in which I have copied an image (called "io.jpg").
I also have checked on build phases -> copy bundle resources and the file is there.
So I am trying to get the path of this image:
NSBundle* bundle=[NSBundle mainBundle];
NSString* path=[bundle pathForResource: @"io" ofType: @"jpg"];
NSLog(@"%@",path);
但是它打印(为空),我也尝试过这种方式:
But it prints (null), I also have tried this way:
NSBundle* bundle=[NSBundle mainBundle];
NSString* path=[bundle pathForImageResource: @"io"];
NSLog(@"%@",path);
但是它仍然打印(空).
有什么问题吗?
But it still prints (null).
What's the problem?
推荐答案
我的猜测是,假设您说您在Xcode项目中创建了一个新的文件夹,那么您已经创建了一个蓝色文件夹引用,您的图片资源位于捆绑软件的子目录中.
My guess, given that you said you created a new folder in your Xcode project is that you have created a blue folder reference and your image resource is in a sub directory of your bundle.
考虑到该类对Foundation框架的重要性,我愿意打赌这不是NSBundle的错误.
I'd be willing to bet it's not a bug with NSBundle, given how old and crutial the class is to the Foundation framework.
尝试使用更具体的实例方法访问您的资源
Try and access your resource using the more specific instance method
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension inDirectory:(NSString *)subpath
其中subpath
是文件夹参考的名称,我猜是您创建的.
where subpath
is the name of the folder reference I am guessing you created.
这篇关于从NSBundle获取零路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!