我想從資源包中從磁盤(文檔,應用程序緩存等)加載plist文件。如何在iOS上從磁盤加載plist文件作爲NSDictionary?
14
A
回答
39
您可以從任何可訪問文件路徑與-initWithContentsOfFile:或+dictionaryWithContentsOfFile:
加載一個的plist從文件加載的plist,如果不存在,創建該文件:
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];
}
23
乾淨了一點:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistName" ofType:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
+0
取決於目錄,documentdirectory!=資源路徑 – M0rph3v5 2015-01-12 10:28:50
相關問題
- 1. iOS版 - 的NSDictionary - 閱讀plist文件
- 2. 如何從磁盤加載字體?
- 3. 下載plist作爲NSDictionary返回(null)
- 4. Swift 3和MacOS:如何直接從磁盤加載文件
- 5. 從磁盤加載程序
- 6. .plist文件如何在iOS中工作
- 7. 從磁盤加載文件,加載一些額外的字符
- 8. 在Django中從磁盤「上傳」文件
- 9. 創建一個NSDictionary作爲Int:String從一個plist文件
- 10. 如何讓Sublime Text 2在磁盤上更改時重新加載文件?
- 11. 如何從視頻文件在磁盤上
- 12. 由plist文件加載的NSDictionary的內存泄露
- 13. 如何在NSDictionary下加載plist並傳遞字符串Array polygons?
- 14. FTP作爲文件夾/網絡磁盤
- 15. 嵌套從plist NSDictionary
- 16. 下載的文件在iOS應用程序的磁盤空間
- 17. APK文件磁盤上不
- 18. 在Android上從本地磁盤加載文件時Javascript無法運行
- 19. 如何將文件直接下載到iPhone OS上的磁盤?
- 20. 在磁盤上寫上傳的文件
- 21. NSDictionary在Linux上加載二進制plist(swift)
- 22. 如何從磁盤
- 23. 無法加載屬性的log4j 2從路徑磁盤文件
- 24. 從C#中的磁盤加載文件 - System.IO.IOException
- 25. TWebbroser凍結應用程序UI - 從磁盤加載HTML文件
- 26. iOS裝置:從plist文件加載字典到陣列
- 27. 從Bundle或磁盤加載更好嗎?
- 28. HttpConnection,UITableView和RunLoops並從磁盤加載
- 29. 快速從磁盤加載UITableViewCell圖像
- 30. 從URL而非文件加載plist
請注意,Plist的根對象必須是字典。如果根對象是一個數組,你應該使用[[NSArray alloc] initWithContentsOfFile:plistFile]; – Ash 2014-01-02 17:36:36