JSON到UITableView由于未捕获的异常终止应用程序' NSInvalidArgumentException'

时间:2015-04-27 12:02:05

标签: ios objective-c json uitableview

我尝试读取文件UITableView并在-(void)loadJSON{ NSString *filePath = [[NSBundle mainBundle] pathForResource:@"countries" ofType:@"json"]; NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil]; NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil]; countries = (NSArray *)jsonObject; NSLog(@"Count of Countries: %ld",[countries count]); } 中显示此数据。通过搜索stackOverFlow,我按照他们在那里所说的那样做了:

NSLog

248打印UITableView,我认为这是正确的值,我假设它正确读取它。 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [countries count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CountryTableCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } cell.textLabel.text = [countries objectAtIndex:indexPath.row]; return cell; } 的两种强制方法也已完成:

-[__NSDictionaryM isEqualToString:]: unrecognized selector sent to instance 0x7fe903e7fb30
2015-04-27 16:51:14.561 appMeNo[10192:1073720] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM isEqualToString:]: unrecognized selector sent to instance 0x7fe903e7fb30'

但是当我运行这个文件时,我收到了这个错误:

tabViewController

我已经采用了很多解决方案,但没有任何效果。我在此应用中使用DBContext。我认为这不会导致问题。

更新
我使用此Github Library OR来获得更清晰的文件图像,这是我复制到项目中的确切文件{{3 }}

4 个答案:

答案 0 :(得分:1)

您正在isEqualToString对象而不是NSDictionary拨打NSString电话。一旦您显示了JSON数据,它就会让我更好地了解到底出了什么问题。

确保在解析之前正确理解您的JSON数据结构。我的猜测是,您在调用loadJSON后无法正确访问它。

编辑:

cell.textLabel.text = [[[countries objectAtIndex:indexPath.row] objectForKey:@"name"] objectForKey:@"common"];

答案 1 :(得分:1)

countries数组具有NSDictionary类型对象,例如,您在indexpath中有country_name 试试这个

cell.textLabel.text = [countries [indexPath.row][@"country_name"]];

答案 2 :(得分:1)

试试这个

cell.textLabel.text = [[[countries objectAtIndex:indexPath.row] objectForKey:@"name"] objectForKey:@"common"];

我希望这段代码对你有用。

答案 3 :(得分:0)

在代码的某处,您正在拨打电话hour = time.localtime().tm_hour 。现在源字符串在那时无效。它一直指向Dictionary对象。

检查生成方法isEqualToString调用的字符串变量的引用。

相关问题