【问题标题】:Reading plist file. iOS Programming读取 plist 文件。 iOS 编程
【发布时间】:2011-11-16 02:45:13
【问题描述】:

我有这段代码,但不知道我做错了什么。正如您在下面的代码中看到的,我有一个名为 shifts.plist 的 plist 文件,它位于我的 supporting files 文件夹中。这是我的 plist 结构。

    NSString *path = [[NSBundle mainBundle] pathForResource:@"shifts" ofType:@"plist"];
    dictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
    cell.textLabel.text = [secondTableInfo objectAtIndex:indexPath.row];
    NSLog(@"%@",[[dictionary objectForKey:@"name"]objectAtIndex:0]);

我最终想阅读name 条目并用它们填充UITableView
我使用NSLog 输出dictionary,得到以下结果。所以文件就在那里 我的解析出错了。

谢谢,
山姆

【问题讨论】:

  • 尝试使用代码删除空格和符号:{ } , = ;
  • 此代码有两个可能的问题:1) 在您的 plist Name 中使用大写 N,而在 objectForKey: 中使用小 n 2) 您尝试调用 objectAtIndex从字典中检索到的对象。由于您已经获得了从字典返回的字符串对象,因此您不需要再调用objectAtIndex:
  • 谢谢,但它仍然返回 null。

标签: ios uitableview plist data-management


【解决方案1】:

看来您需要先致电objectAtIndex:,然后再致电objectForKey:

例如:

[[dictionary objectAtIndex:0] objectForKey:@"Name"]

【讨论】:

  • 它只会让应用程序崩溃。还有其他想法吗?
  • 试试[[[dictionary objectForKey:@"Root"] objectAtIndex:0] objectForKey:@"Name"]
【解决方案2】:

主要错误:- 根是数组,您正在将文件放入字典中。因此在 .h 文件中声明一个 NSArray 并保留其属性,非原子性。

NSString *path = [[NSBundle mainBundle] pathForResource:@"shifts" ofType:@"plist"];

array = [[NSArray alloc]initWithContentsOfFile:path];

NSLog(@"First Index Name %@",[[array objectAtIndex:0] objectForKey:@"Name"]);

我相信它会回答你的问题。

【讨论】:

  • 感谢您的回答。我将如何阅读多个名字?我想我必须更改 array objectAtIndex:0 部分,但不太确定要更改什么。干杯
  • 使用 indexPath.row 代替 0 你会得到想要的结果。欢迎提出任何问题
猜你喜欢
  • 1970-01-01
  • 2012-02-04
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多