NSBundle pathForResource is NULL
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.
- Select your project
- Select Target
- Select Build Phases tab
- Open the section labelled "Copy Bundle Resources"
- 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")
Comments
-
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:
-
patrick over 13 yearsthanks for reply, but files seem to be added to the target -> grab.by/6TG4
-
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, trybundleForClass:[SomeClassFromApi class]
. -
patrick over 13 yearsit's the same :( I you want I can send you the xcode project.
-
DarkDust over 13 yearsI normally don't do that, but OK. You can find my e-mail address at the bottom of my homepage.
-
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 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 about 12 yearsJust this line of your answer helped me "Is the file really included in the target?" Thank you!
-
raffian almost 12 yearsI'm using the project type
Command-line Tool
as well and I can't get a file to load fromNSBundle pathForResource
. Should I switch toCocoa Application
project type? I'm not sure I follow your suggestion of adding a newApplication
target; you mean add a new target in the existing Command-line tool project? -
DarkDust almost 12 yearsYes, 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 almost 11 yearsCommand-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 almost 10 yearsI'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. over 9 yearsWell, consistent casing. Not necessarily 'small' but yep. That was the problem for me. :)
-
oky_sabeni about 9 yearsAdd to the list that xcode does not automatically are .epub files
-
Klaas almost 9 yearsThere is a build setting called
Combine high resolution artwork
/COMBINE_HIDPI_IMAGES
to disable this. -
Jagveer Singh almost 9 yearsgreat answer @DarkDust
-
sparkonhdfs almost 8 yearsXcode also does not include shell scripts in resources automatically.
-
Ed of the Mountain over 7 yearsThis 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 over 6 yearsThis works if I build my project using Xcode, but not using
swift build
. Any solution? -
fawsha1 over 6 years@JayWong4 What happens using the build command? Are you getting an error message?
-
Kasper over 5 yearsit 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?