“[__ NSArrayM objectForKey:]:无法识别的选择器”在尝试从Dictionary中检索字符串时

时间:2013-09-11 14:25:59

标签: ios nsstring nsarray nsdictionary nsexception

大家好我正在尝试将文字设置为我的标签。这是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    newsDataCell *cell = (newsDataCell*)[tableView dequeueReusableCellWithIdentifier:@"newsData"];

    if (cell == nil)
    {
        cell = [[newsDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newsData"];
    }

   NSDictionary* currentrow =[self.Items objectAtIndex:indexPath.item];
     NSLog(@"currentrow:%@",currentrow);
    //UIImageView *image = (UIImageView*)[cell viewWithTag:0];

    UILabel *txtData = (UILabel*)[cell viewWithTag:1];
    NSString *testo = [currentrow objectForKey:@"Date"];
    [txtData setText:testo];


    UILabel *txtTitolo = (UILabel*)[cell viewWithTag:2];
    txtTitolo.text =[currentrow valueForKey:@"Title"];

    UILabel *txtDescrizione = (UILabel*)[cell viewWithTag:3];
    txtDescrizione.text = [currentrow valueForKey:@"Descr"];

    return cell;
}

我检查过,currentrow实际上包含了这个对象。 这是我从Xcode得到的错误:

2013-09-11 16:04:36.468 ApplicationName [2409:c07] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSArrayM objectForKey:]:无法识别的选择器发送到实例0x7172f80' * 第一次抛出调用堆栈: (0x1333012 0x1158e7e 0x13be4bd 0x1322bbc 0x132294e 0xc5f4 0x1578fb 0x1579cf 0x1401bb 0x150b4b 0xed2dd 0x116c6b0 0x265bfc0 0x265033c 0x2650150 0x25ce0bc 0x25cf227 0x25cf8e2 0x12fbafe 0x12fba3d 0x12d97c2 0x12d8f44 0x12d8e1b 0x22757e3 0x2275668 0x9cffc 0x2c2d 0x2b55) libc ++ abi.dylib:terminate调用抛出异常

我使用valueForKey得到了同样的错误:

txtTitolo.text =[currentrow valueForKey:@"Title"];

这是:

2013-09-11 16:09:34.478 ApplicationName [2409:c07] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSArrayM valueForKey:]:无法识别的选择器发送到实例0x7172f80' * 第一次抛出调用堆栈: (0x1333012 0x1158e7e 0x13be4bd 0x1322bbc 0x132294e 0xc5f4 0x1578fb 0x1579cf 0x1401bb 0x150b4b 0xed2dd 0x116c6b0 0x265bfc0 0x265033c 0x2650150 0x25ce0bc 0x25cf227 0x25cf8e2 0x12fbafe 0x12fba3d 0x12d97c2 0x12d8f44 0x12d8e1b 0x22757e3 0x2275668 0x9cffc 0x2c2d 0x2b55) libc ++ abi.dylib:terminate调用抛出异常

出了什么问题?谢谢!!

4 个答案:

答案 0 :(得分:0)

一个。此错误不是由Xcode产生的,而是由运行时环境产生的。

B中。这意味着currentrow不是字典而是数组。

℃。你是怎么检查的?你在哪里创建项目并将其放在列表中?

答案 1 :(得分:0)

看起来currentrow不是NSDictionary(您假设的)。这是一个NSArray

NSDictionary* currentrow =[self.Items objectAtIndex:indexPath.item];
// Not a dictionary, it is an array.

这就是你遇到这个崩溃的原因。如果这是解析的JSON响应,请检查JSON的结构。

希望有所帮助!

答案 2 :(得分:0)

我做到了!我刚刚使用了“stringWithFormat”,它的工作原理如下:

NSString *testo = [NSString stringWithFormat:@"%@",[currentrow objectForKey:@"Date"]];

无论如何,谢谢你的回答!

答案 3 :(得分:0)

对我而言,这是有效的。

customerBillerFieldsArray = [self.detailItem valueForKey:@"customerBillerFileds"];

之前我使用过customerBillerFieldsArray = [self.detailItem objectForKey:@"customerBillerFileds"];

相关问题