0

我正在从 plist 加载数据,并试图找出如何访问每个项目的数据。在下面的代码中,我希望能够提取文本的值以及它是检查 = 0 还是检查 = 1

我试过这个:

    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 等等......然后使用它来将参数传递给字符串。

4

2 回答 2

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);
}
于 2013-01-21T15:32:02.710 回答
1

像这样的东西,我猜:

for (id dict in dataArray)
  int checked = [[(NSDictionary *)dict objectForKey:@"checked"] intValue];
于 2013-01-21T15:30:21.963 回答