簡體   English   中英

通過主捆綁包vs文檔目錄訪問plist文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM