簡體   English   中英

我可以從一個plist讀取,但不能從另一個列表讀取

[英]I can read from a plist but fail to read from another one

我在“支持文件”中創建了兩個名為info.plist和data.plist的plist文件,並將它們都添加到“復制捆綁包資源”中。

我使用下面的代碼從plist讀取數據。

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory , NSUserDomainMask , YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary* dictPlist = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:filePath];

並嘗試顯示plist中的數據:

NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@", data1);

我從information.plist獲取數據,但從data.plist獲取“空”。

我錯過了什么?

感謝您的幫助!

嘗試這個:

// Path of plist from bundle
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

// Since your plist's root is a dictionary else you should form NSArray from contents of plist
NSDictionary *dataPlist = [NSDictionary dictionaryWithContentsOfFile:plistPath];

說明:您的plists在應用程序捆綁包中,而不在Documents目錄中,這就是為什么您沒有得到它們的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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