pathForResource returns null for plist

2019-09-19 08:06发布

问题:

I'm trying to get the path for a file named data.plist that is in my project. I have this code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSLog(@"Path: %@",path);

That prints "Path: (null)" in the console. The plist is in the Copy Bundle Resources build phase, so that's not the problem. The plist is one that was pre-made for a class project, and I believe I am adding it to my XCode project properly.

If any more code snippets would be helpful in answering the question, I'll provide them.

回答1:

Just because it's in the copy bundle resources build phase doesn't guarantee that it is in the correct place in your app, and that it is named correctly.

If the pathForResource:ofType: call returns nil, the file isn't in the bundle at the right place with the right name.

I suggest you do the following: Right-click on the app in the project organizer in Xcode and select "Show in finder.

Right-click on the app, open it, and examine the "contents" folder. See if your plist file is really there. Also double-check the case of both the filename and the extension.