2013-01-21 52 views
0

我正在從plist加載數據,並試圖找出如何訪問每個項目的數據。在下面的代碼我希望能夠提取文本的價值,不管是檢查= 0或檢查= 1如何從plist中加載值?

我已經試過這樣:

NSString *dataArray1 = [[dataArray objectAtIndex:1] objectAtIndex:2]; 

但不知道是否是最好的辦法

感謝您的任何幫助。

// load data from a plist file inside our app bundle 
NSString *path = [[NSBundle mainBundle] pathForResource:@"Providers" ofType:@"plist"]; 
dataArray = [NSMutableArray arrayWithContentsOfFile:path]; 

NSLog(@"data array from offers %@", dataArray); 

下面是輸出:

2013-01-21 15:13:34.599 data array from offers (
     { 
     checked = 1; 
     text = Provider1; 
    }, 
     { 
     checked = 1; 
     text = Provider2; 
    }, 
     { 
     checked = 1; 
     text = Provider3; 
    }, 
     { 
     checked = 1; 
     text = Provider4; 
    } 
) 

所以我希望能夠找出每個項目的檢查值。 Provider4設置爲1,Provider3爲0等...然後使用它將參數傳遞給字符串。

回答

3
NSString *path = [[NSBundle mainBundle] pathForResource:@"Providers" ofType:@"plist"]; 
dataArray = [NSMutableArray arrayWithContentsOfFile:path]; 
for (NSDictionary *dictionary in dataArray) 
    { 
    NSString *text = [dictionary valueForKey:@"text"]; 
    NSNumber *checked = [dictionary valueForKey:@"checked"]; 
    NSLog(@"%@ checked value is: %@", text, checked); 
} 
+0

完美!謝謝:) – hanumanDev

+0

任何想法如何我可能會要求只顯示提供商檢查== 1? – hanumanDev

1

這樣的事情,我想:

for (id dict in dataArray) 
    int checked = [[(NSDictionary *)dict objectForKey:@"checked"] intValue];