2013-12-16 221 views
0

我無法讀取plist文件中的數據。我沒有像我期待的那樣獲得在控制檯中打印的titleString。我究竟做錯了什麼? enter image description here無法讀取plist文件

NSString *path = [[NSBundle mainBundle] pathForResource:@"Events" ofType:@"plist"]; 
    NSDictionary *dictPri = [[NSMutableDictionary alloc]initWithContentsOfFile:path]; 
    NSMutableArray *arrEvents = [[NSMutableArray alloc] initWithArray:[dictPri valueForKey:@"Root"]]; 

    for (NSDictionary *dict in arrEvents) 
    { 
     NSString *titleString = nil; 
     NSString *date = nil; 
     titleString = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Title"]]; 
     date = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Date"]]; 

     NSLog(@"Title String: %@", titleString); 
    } 

回答

2

你的主要元素(根)是詞典 - 不是陣列 - 通過單擊旁邊的類型更改它的plist。

另外你的代碼有問題 - 你永遠不會按名稱訪問「Root」元素 - 它是默認的頂級對象。考慮取出不需要的額外數組初始化。

固定碼:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Events" ofType:@"plist"]; 
NSArray* arrEvents = [NSArray arrayWithContentsOfFile:path]; 

for (NSDictionary *dict in arrEvents) 
{ 
    NSString *titleString = [NSString stringWithFormat:@"%@",[dict objectForKey:@"Title"]]; 
    NSString *date = [NSString stringWithFormat:@"%@",[dict objectForKey:@"Date"]]; 

    NSLog(@"Title String: %@", titleString); 
} 
+0

餘留根作爲字典和根內建立一個數組,並添加了項目的該陣列。謝謝! – raginggoat