I want to load the plist file from disk (documents, application cache, ...) not from a resource bundle.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can load a plist from any accessible file path with -initWithContentsOfFile: or +dictionaryWithContentsOfFile:
Load a plist from a file, and create the file if it did not exist:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
self.plistFile = [[paths objectAtIndex:0]
stringByAppendingPathComponent:@"example.plist"];
self.plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile];
if (!plist) {
self.plist = [NSMutableDictionary new];
[plist writeToFile:plistFile atomically:YES];
}
回答2:
A little cleaner:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistName" ofType:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];