问题
I have an iPhone app that does XML parsing from a URL. I have a sample.xml file in my Resources directory (in XCode) that I'd like to use.
How do I reference this file in code? I've tried @"sample.xml" as the URL and it doesn't appear to be able to find it.
回答1:
First off, be careful passing a URL as a path and vice-versa - you may not be able to readily interchange the two. If you must, try prefixing the path of the file with file://
.
As for finding the path, you can use the NSBundle method pathForResource:ofType:
as such:
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"sample"
ofType:@"xml"];
NSString *xmlFileContents = [NSString stringWithContentsOfFile:xmlFilePath];
来源:https://stackoverflow.com/questions/1904527/iphone-access-local-xml-file