1

我有这段代码,无法弄清楚我做错了什么。正如您在下面的代码中看到的那样shifts.plist,我的supporting files文件夹中有一个名为 plist 的文件。这是我的 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条目并用它们填充 a UITableView
我曾经NSLog输出dictionary,我得到了以下内容。所以文件在那里,只是我弄错了解析。
NSLog 输出
谢谢,
山姆

4

2 回答 2

1

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

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

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

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

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

于 2011-11-18T09:59:17.670 回答
1

看起来你需要先打电话objectAtIndex:,然后打电话objectForKey:

例如:

[[dictionary objectAtIndex:0] objectForKey:@"Name"]
于 2011-11-16T03:49:33.017 回答