' - [__ NSCFString objectForKey:]:无法识别的选择器发送到实例0x9c515c0'

时间:2014-05-23 13:12:04

标签: ios objective-c nsarray

问题是------我从url获取数据和arrayobj中的数据。当我运行此项目时,我收到此错误。我在项目中做错了语句或代码请检查{{1 }和connectiondidfinishloading

cellforrowanindexpath

//这是索引路径代码

的行的单元格
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error ;
NSMutableDictionary *dictobj = [NSJSONSerialization JSONObjectWithData:dataobj options:NSJSONReadingMutableContainers error:&error];

newsobj=[dictobj objectForKey:@"news"];

//NSArray *longdeceobj=[newsobj objectForKey:@"long_desc"];

 for (NSDictionary *dict in  newsobj)
 {
    NSDictionary *title=[dict objectForKey:@"title"];
    [arrayobj addObject:title];
 }
 [[self tableobj]reloadData];
}

错误是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellide=@"identifier";
newcustomobj=(newsCustamView *) [tableView dequeueReusableCellWithIdentifier:cellide];
if (newcustomobj==nil)
{
    [[NSBundle mainBundle]loadNibNamed:@"newsCustamView" owner:self options:nil];
}
newcustomobj.newstitle.text=[[[arrayobj objectAtIndex:0]objectForKey:@"news"]objectForKey:@"title"];
return newcustomobj;
}

4 个答案:

答案 0 :(得分:5)

您假设的变量dictobj是NSDictionary,实际上它是NSString。由于字符串不响应objectForKey:方法,因此您的应用程序崩溃了。这就是错误消息告诉你的内容。检查此link。了解更多细节。

答案 1 :(得分:1)

您解决的其中一个对象(您认为它是字典)实际上是一个字符串。

特别是在解析JSON或XML时:始终仔细检查您获取并继续使用的对象的类。使用[object isKindOfClass:[ClassIThinkItIs class]]

答案 2 :(得分:0)

cell.lblTitle.text = [[dataObjectArray objectAtIndex:indexPath.row]title];

cell.datobj.text = [[dataObjectArray objectAtIndex:indexPath.row]date];

cell.longdicobj.text = [[dataObjectArray objectAtIndex:indexPath.row]long_desc];

[cell.imageobj setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[dataObjectArray objectAtIndex:indexPath.row]image_file]]]
      placeholderImage:[UIImage imageNamed:@"songs85-56.png"]];

答案 3 :(得分:0)

我认为json数据返回是news密钥字典的新闻字典数组,每个news字典都有一个title密钥。因此,您可以尝试使用以下代码在title中显示cellForRow

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error ;
NSMutableDictionary *dictobj = [NSJSONSerialization JSONObjectWithData:dataobj options:NSJSONReadingMutableContainers error:&error];

newsobj=[dictobj objectForKey:@"news"];

//NSArray *longdeceobj=[newsobj objectForKey:@"long_desc"];

 for (NSDictionary *dict in  newsobj)
 {
    NSString *title=[dict objectForKey:@"title"]; //title may be NSString type
    [arrayobj addObject:title];
 }
 [[self tableobj] reloadData];
}
在CellForRow中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellide=@"identifier";
newcustomobj=(newsCustamView *) [tableView dequeueReusableCellWithIdentifier:cellide];
if (newcustomobj==nil)
{
    [[NSBundle mainBundle]loadNibNamed:@"newsCustamView" owner:self options:nil];
}
newcustomobj.newstitle.text= [arrayobj objectAtIndex:0]; //just get title of first news, you can replace 0 with indexpath.row to show news for row.
return newcustomobj;
}