0
votes

In my code I want to open a html file, so added the following line of code:

NSString* path = [[NSBundle mainBundle] pathForResource:@"cheese" ofType:@"html"];

But it fails as path is nil after execution. The file cheese.html exists and has been added to the project's resources.

I couldn't, and still can't figure out why its not working. I have a few other projects that open and read files so as an experiment I copied the line of code that is failing to one of these other projects, and along with it I dragged/dropped the cheese.html file from the project where it doesn't work to this other project (dragged and dropped from within xcode's hieararchical project file view), and bingo - the line executes and path is not nil in that other project.

So I'm totally mystified, why is it working in one project but not another?

Its the exact same line of code in both, and as the file was dragged and dropped its the same file, and as the file was dragged from within xcode itself, it proves the file has been added to the projects resources. So why would it still fail?

2
I gave up after a few hours, scrapped the project and took the one that worked and removed all the bits that weren't relevant. No idea why it never worked.Gruntcakes
Have you checked to make the file is physically in the right place including and not residing in another subfoder,etc.user523234

2 Answers

2
votes

Check this:

In the left hand organizer select your project. Then in the next navigator over Select your target name. Then Select the build phases 'tab'. You will see a section 'Copy Bundle Resources'. Make sure the resource you are looking for is listed there. If it is not add it.

2
votes

Another possible solution to this problem for anyone who is experiencing it: Sometimes a file can belong to a bundle which is not the mainBundle - in this case the easiest solution is to use [NSBundle bundleForClass:[self class]] instead, to use the bundle that the current class is using.