NSBundle pathForResource is NULL

44,293

Solution 1

So here's the solution for this problem after I got the source:

I didn't really pay attention to the posted screenshot, but the target is of type "Command-line Tool"... and since those don't have a bundle [NSBundle mainBundle] of course returns nil. It's pretty misleading that Xcode doesn't complain that it can't execute the "Copy Bundle Resources" step, it just silently skips it.

Solution is simply to add a new target, of type "Application" so a bundle-based application is generated. Then check the Target Membership checkboxes for all sources and resources for this new target. The plist paths are correctly resolved then.

Solution 2

I was trying to get my iPhone app to use a default sqlite database and the darn app couldn't find it. Turned out that I had to make sure that the .sqlite file was in the bundle resource.

  1. Select your project
  2. Select Target
  3. Select Build Phases tab
  4. Open the section labelled "Copy Bundle Resources"
  5. Drag and drop your .sqlite file into this section.

now your app will find this default sqlite database.

Solution 3

Is the file really included in the target (and will therefor be copied to the bundle) ? There two ways to find out/set that:

First way: right-click (or Cmd-click) on the file, select "Get Info". Then click on the "Targets" tab and make sure the file is checked for the desired target(s).

Second way: right-click (or Cmd-clock) in the project browser on the header of the file browser (it will likely read "Groups & Files"). Then select "Target Membership". Now you have checkboxes next to each file that can be member of a target (like .m files or resources). Again, make sure the checkbox next to your file is checked.

Solution 4

Since I have googled here, did not find the answer, but then discovered it by myself, I'll leave it here...

I had 2 files: tray.png and [email protected] for Retina. The files were added to "Copy Bundle Resources" automatically.

But:

[[NSBundle mainBundle] pathForResource:@"tray" ofType:@"png"];

did not return the file actually copied to the bundle! The reason was: IDE created one TIFF file tray.tiff (joint tray.png and [email protected]), so ... ofType:@"tiff"] helped!

Solution 5

My problem and solution are very similar to Dmitriy Isaev's ones. I have tried to get path for a xib file. Both calls (in Swift) pathForResource("myfile", ofType: "xib") and pathForResource("myfile.xib", ofType: nil) are failed.

The solution is to use extension "nib" instead:

pathForResource("myfile", ofType: "nib")
Share:
44,293
patrick
Author by

patrick

Doing things, for reasons.

Updated on October 01, 2020

Comments

  • patrick
    patrick over 3 years

    I'm creating a simple application with xcode and objc and I need to load an NSDictionary from a file, but I can't get the path to the file using NSBundle:

    NSString *l = [[NSBundle mainBundle] pathForResource:@"LoginStatuses" ofType:@"plist"];
    NSLog(@"%@", l);
    

    When I run this code I get this:

    2010-10-16 10:42:42.42 Sample[5226:a0f] (null)

    And I don't know why.

    I created a group called Resources and there I added the LogingStatuses.plist: res

  • patrick
    patrick over 13 years
    thanks for reply, but files seem to be added to the target -> grab.by/6TG4
  • DarkDust
    DarkDust over 13 years
    @Patrick: Your target is called MetwitApi... does that mean that you use that bundle in some other application ? If so, then mainBundle would be wrong... instead, try bundleForClass:[SomeClassFromApi class].
  • patrick
    patrick over 13 years
    it's the same :( I you want I can send you the xcode project.
  • DarkDust
    DarkDust over 13 years
    I normally don't do that, but OK. You can find my e-mail address at the bottom of my homepage.
  • Joshua Nozzi
    Joshua Nozzi over 13 years
    @Patrick: A quick word on etiquette. People volunteer their time on sites and mailing lists to help the community as much as individuals. It doesn't help any future visitors to the site who find your question because they had the same problem when you post publicly then take things offline. It's also considered bad form to "latch on" to an individual with the burden of single-handedly helping you through your problem. Let the community help by keeping the question and answer in the community. That way you'll earn a good reputation and help the helpers do the same.
  • Marcel Falliere
    Marcel Falliere over 13 years
    +1 for this one - instead of creating a new target application, I prefer adding existing file to the main application bundle.
  • Hamed Rajabi
    Hamed Rajabi about 12 years
    Just this line of your answer helped me "Is the file really included in the target?" Thank you!
  • raffian
    raffian almost 12 years
    I'm using the project type Command-line Tool as well and I can't get a file to load from NSBundle pathForResource. Should I switch to Cocoa Application project type? I'm not sure I follow your suggestion of adding a new Application target; you mean add a new target in the existing Command-line tool project?
  • DarkDust
    DarkDust almost 12 years
    Yes, I meant to add a new target (of type Application) to your existing project. A normal command line tool is just an executable without a bundle. Any resources you add to your Command-line Tool target is simply ignored (but Xcode doesn't warn you about that).
  • capikaw
    capikaw almost 11 years
    Command-line Tool targets can still link files using NSBundle pathForResource. You need tell the application to copy the file(s) into the products directory when it builds. Go into “Build Phases” tab, click “Add Build Phase”, and select “Add Copy Files”. Then simply drag the file(s) you need.
  • dave
    dave almost 10 years
    I've found that Xcode adds certain types of files to the Copy Bundle Resources phase automatically, and leaves others out. For example, .html files (along with .strings, .png, etc.) are added automatically, but .js files are not.
  • Ev.
    Ev. over 9 years
    Well, consistent casing. Not necessarily 'small' but yep. That was the problem for me. :)
  • oky_sabeni
    oky_sabeni about 9 years
    Add to the list that xcode does not automatically are .epub files
  • Klaas
    Klaas almost 9 years
    There is a build setting called Combine high resolution artwork / COMBINE_HIDPI_IMAGES to disable this.
  • Jagveer Singh
    Jagveer Singh almost 9 years
    great answer @DarkDust
  • sparkonhdfs
    sparkonhdfs almost 8 years
    Xcode also does not include shell scripts in resources automatically.
  • Ed of the Mountain
    Ed of the Mountain over 7 years
    This solved my problem after upgrading to Xcode 8.1. *** Warning: There is no command line tools for Xcode 8.1 when installed under OS X 10.1.66.
  • Jay Wong
    Jay Wong over 6 years
    This works if I build my project using Xcode, but not using swift build. Any solution?
  • fawsha1
    fawsha1 over 6 years
    @JayWong4 What happens using the build command? Are you getting an error message?
  • Kasper
    Kasper over 5 years
    it works for me too. but I'm wondering if 1) is this clean? I've already a Copy File Phase. 2) wouldn't be cleaner to create another bundle (Resource.bundle), put there all the resources and link the CLP with the resource.bundle?