views:

52

answers:

2

Hi, 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

Can you help me?

Thanks :)

+2  A: 

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.

DarkDust
+1 it's probably this, I have been bitten by this one too
invariant
thanks for reply, but files seem to be added to the target -> http://grab.by/6TG4
patrick
@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]`.
DarkDust
it's the same :( I you want I can send you the xcode project.
patrick
I normally don't do that, but OK. You can find my e-mail address at the bottom of my homepage.
DarkDust
@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.
Joshua Nozzi
+2  A: 

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.

DarkDust
Thank you very much, it worked! :D
patrick