NSBundle pathForResource is NULL

Viewed 44601

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

10 Answers

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.

There is a way to do this for a Command-Line app.

Instead of using "Copy Bundle Resources" use "Copy Files". For "Destination" select "Resources". This will copy the file to the app bundle and the Bundle.main can find it.

Related