objective-cnsbundlexcode4.4

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?


Solution

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