如何从plist加载值?

时间:2013-01-21 15:24:42

标签: iphone ios objective-c plist

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

2 个答案:

答案 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);
}

答案 1 :(得分:1)

类似这样的东西,我想:

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