如何獲取此plist的內容?獲取plist的內容
我對於不同的元素以及如何訪問它們有點困惑。
一個人怎麼訪問這些字典和詞典在詞典?
如果我想先複製這些數據,它會提高很多性能,而不是總是從plist讀取數據?
通過檢查結構級別,我怎樣才能將所有這些數據存入我的應用程序?
謝謝
如何獲取此plist的內容?獲取plist的內容
我對於不同的元素以及如何訪問它們有點困惑。
一個人怎麼訪問這些字典和詞典在詞典?
如果我想先複製這些數據,它會提高很多性能,而不是總是從plist讀取數據?
通過檢查結構級別,我怎樣才能將所有這些數據存入我的應用程序?
謝謝
你應該看看NSPropertyListSerialization class,這使得分析的Plist相對簡單。
這裏的一個比特的示例代碼:http://iphoneincubator.com/blog/tag/nspropertylistserialization
讀取屬性列表中的最簡單的方法是通過使用上NSDictionary
一個方便的方法(或者NSArray
如果您的根元素是一個數組)像這樣:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"FileName"
ofType:@"plist"];
NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:filePath];
從那裏的plist文件只是一個正常的字典。使用objectForKey:
等來獲得值。對於深子節點更快的訪問,您可以使用valueForKeyPath:
像這樣的例子:
NSString* name = [plist valueForKeyPath:@"mainDictionary.name"];
有關的plist文件更復雜的操作,你應該使用NSPropertyListSerialization
有幾類方法更多的更細粒度的訪問讀取和寫入性能列表文件。
use this :
NSDictionary *dic1 = [[NSDictionary alloc] initWithContentofFile:yourFilePathhere];
// return main dictionary
NSDictionary *dic2 = [dic1 objectForKey:mainDictionary];
let filePath:String = NSBundle.mainBundle().pathForResource("infoList_English", ofType: "plist")!
let postDictionary : NSDictionary = NSDictionary(contentsOfFile: filePath)!
print(postDictionary)