我正在从plist加载数据,并试图找出如何从每个项目访问数据。在下面的代码中,我希望能够提取text的值以及其是否被选中= 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,依此类推...然后使用该参数将参数传递给字符串。

最佳答案

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

关于iphone - 如何从plist加载值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14441821/

10-14 23:34