[英]Accessing plist file by main bundle vs Documents directory
獲得財產清單的最佳方法是什么?
1-來自主捆綁包,即:
NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"];
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"];
}
什么,為什么? 如果兩者都是正確的,那為什么我們只用一行代碼就不使用第一個代碼呢?
簡單規則:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.