Iphone 带有JSON的Master Detail应用程序给出了未捕获的异常_NSCFDictionary objectAtIndexedSubscript
我想用JSON数据(已经过验证)填充一个master detail应用程序,并得到错误“uncaught exception\uu NSCFDictionary objectAtIndexedSubscript”。非常感谢 JSON文件:Iphone 带有JSON的Master Detail应用程序给出了未捕获的异常_NSCFDictionary objectAtIndexedSubscript,iphone,objective-c,json,xcode4,nsdictionary,Iphone,Objective C,Json,Xcode4,Nsdictionary,我想用JSON数据(已经过验证)填充一个master detail应用程序,并得到错误“uncaught exception\uu NSCFDictionary objectAtIndexedSubscript”。非常感谢 JSON文件: { "Name": [ "Entry 1 (Comment1) (Comment1b)", "Entry 2 (Comment2) ", "Entry 3 (Comment3) ", "Entry 4 (Comment4
{
"Name": [
"Entry 1 (Comment1) (Comment1b)",
"Entry 2 (Comment2) ",
"Entry 3 (Comment3) ",
"Entry 4 (Comment4) (Comment4b)"
],
"URLs": [
"http://www.myurl.com/%20(Comment1)%20(Comment1b)",
"http://www.myurl.com/%20(Comment2)%20(Comment2b)",
"http://www.myurl.com/%20(Comment3)%20(Comment3b)",
"http://www.myurl.com/%20(Comment4)%20(Comment4b)"
]
}
@interface MasterViewController () {
NSArray *_objects;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://myurl.com/Data.json"];
NSData *data = [NSData dataWithContentsOfURL:url];
_objects = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
以下是我加载JSON文件的方式:
{
"Name": [
"Entry 1 (Comment1) (Comment1b)",
"Entry 2 (Comment2) ",
"Entry 3 (Comment3) ",
"Entry 4 (Comment4) (Comment4b)"
],
"URLs": [
"http://www.myurl.com/%20(Comment1)%20(Comment1b)",
"http://www.myurl.com/%20(Comment2)%20(Comment2b)",
"http://www.myurl.com/%20(Comment3)%20(Comment3b)",
"http://www.myurl.com/%20(Comment4)%20(Comment4b)"
]
}
@interface MasterViewController () {
NSArray *_objects;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://myurl.com/Data.json"];
NSData *data = [NSData dataWithContentsOfURL:url];
_objects = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
我就是这样分析它的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"_objects contains: %@", _objects);
// # This following line creates the error:
NSDictionary *object = _objects [indexPath.row];
NSLog(@"object contains: %@", object);
cell.textLabel.text = [object objectForKey: @"Name"];
cell.detailTextLabel.text = [object objectForKey: @"URLs"];
return cell;
}
您以错误的方式访问JSON字典。顶级对象是字典,但您将它们视为数组,并尝试通过indexPath.row加载它们 这样的方法应该可以奏效:
cell.textLabel.text = _objects[@"Name"][indexPath.row];
cell.detailTextLabel.text = _objects[@"URLs"][indexPath.row];
爆发时,看起来是这样的:
NSArray *nameArray = _objects[@"Name"];
NSArray *urlArray = _objects[@"URLs"];
cell.textLabel.text = nameArray[indexPath.row];
cell.detailTextLabel.text = urlArray[indexPath.row];
感谢@danielbeard的回复,但现在我在这两行上收到了一个编译器警告:“读取'NSArray*'类型的对象上未找到的dictionary元素的预期方法更改为NSDictionary类型谢谢@danielbeard,但这现在给了我“tableView DidSelectRowatineXpath”和“prepareForSegue”行中的错误:”“读取“NSDictionary*”类型的对象上未找到的数组元素的预期方法”您还必须更改这些相应的方法,以便它们使用与上述代码类似的方法(如果您尝试从
\u对象中读取内容
)。