[英]Accessing plist file by main bundle vs Documents directory
What is the best way to get property list? 获得财产清单的最佳方法是什么?
1 - From main bundle, ie: 1-来自主捆绑包,即:
NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"];
2 - Or get path from Documents directory, ie: 2-或从Documents目录获取路径,即:
// Data.plist code - get path from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our plist file.
NSString *plistPath = [documentsPath
stringByAppendingPathComponent:@"Data.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
What and why? 什么,为什么? And if both are correct then why do we not use 1st one with just one line of code?
如果两者都是正确的,那为什么我们只用一行代码就不使用第一个代码呢?
Simple rule: 简单规则:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.