NSBundle pathForResource is NULL

2020-01-26 06:43发布

问题:

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:

回答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.



回答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.



回答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.



回答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 tray@2x.png 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 tray@2x.png), so ... ofType:@"tiff"] helped!



回答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")


回答6:

I encountered this issue today with a Command Line project.

Luckily, the solution is easy. Simply go to "Build Phases", click on "+" and add a new "Copy Files" phase. Choose "Resources" as Destination, and add any files you want to use.

Now you can still use [NSBundle mainBundle] and it should work!



回答7:

Filename is case sensitive on iPad. You need use small letters.



回答8:

In my case (executing XCTestCase) for some reason resources were stored in non-main Bundle. I fixed the problem by checking first which bundle test class belongs to:

[[NSBundle bundleForClass:[self class]] pathForResource:@"Config" ofType:@"plist"];

Hopefully this can help someone else as well.



回答9:

Make sure you spell your resource's file name properly. I just learned that the hard way. :)