Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a nil path from NSBundle

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?

like image 347
Ramy Al Zuhouri Avatar asked Jun 09 '26 10:06

Ramy Al Zuhouri


2 Answers

Go to : Target -> "Build Phases" -> "copy bundle Resources" Then add that particular file here.

clean the project and RUN. It works. :)

like image 81
seenu Avatar answered Jun 11 '26 22:06

seenu


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.

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

where subpath is the name of the folder reference I am guessing you created.

like image 37
Jessedc Avatar answered Jun 11 '26 21:06

Jessedc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!