iphone - 如何从 plist 加载值?

标签 iphone ios objective-c plist

我正在从 plist 加载数据,并试图了解如何访问每个项目的数据。在下面的代码中,我希望能够提取文本的值以及它是 checked = 0 还是 checked = 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/

相关文章:

objective-c - PresentModalViewController 稍后才会触发

ios - 在 Xcode 中运行 iOS 单元测试时引用 _main 的链接器错误

iphone - Xcode - 根据正在编译代码的设备修改库搜索路径

iphone - 如何查看 In App Purchase Auto Renewable Subscription 是否有效

ios - 如何将每个用户链接到他们在 Firebase for Swift 3 中的数据?

ios - JSON页面仅访问第一页xcode

iphone - 用于iphone sdk开发的图像过滤器

iphone - UIInterfaceOrientationLandscapeLeft 和 UIInterfaceOrientationLandscapeRight 颠倒了?

ios - HealthKit 在 Swift 2.0 中仅发布一次

ios - OCMock3 与 IOS 的集成问题