0

当我在 aepub 阅读器中运行搜索功能时,我的应用程序崩溃了。它以 index 方法进入 cellfor 行,执行NSLOg(@"%@",hit.neighbourText)时显示异常。

    (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        cell.textLabel.adjustsFontSizeToFitWidth = YES;

        NSLog(@"indexpath%d",indexPath.row);
        NSLog(@"%@",[results objectAtIndex:[indexPath row]]);

        hit = (SearchResult*)[results objectAtIndex:[indexPath row]];

        if([results count]>0) {
            NSLog(@"%@",hit.neighboringText);
            cell.textLabel.text = [NSString stringWithFormat:@"...%@...", hit.neighboringText];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"Chapter %d - page %d", hit.chapterIndex, hit.pageIndex+1];

           return cell;
        }
    }

我得到了一些价值,hit.neighboringText但在那之后,我重新加载了我的 tableview 然后会引发以下异常,为什么?

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFConstantString neighborText]:无法识别的选择器发送到实例 0x1481c4”*** 第一次抛出调用堆栈:

4

4 回答 4

5

这是因为hit它实际上是一个NSString对象,而不是SearchResult您期望的对象:

hit = (SearchResult*)[results objectAtIndex:[indexPath row]];

线索在异常文本中:

-[__NSCFConstantString neighboringText]: unrecognized selector sent to instance ...
  ^^^^^^^^^^^^^^^^^^^^                   ^^^^^^^^^^^^^^^^^^^^^

再多的演员SearchResult也不会改变这一点。

编辑:实际上,在您看到演员表的任何地方,您都应该怀疑您正在处理的实际对象。如果您不确定,请使用isKindOfClass:.

于 2013-05-17T06:43:34.390 回答
0

I guess there are two possibilities:

  • hit is not a SearchResult object but a String object
  • hit or the results array is not owned anymore / released but not set to nil and point garbage, which I believe is the case because I have experienced it before

I think you need to make sure that the array is not autoreleased/released at that point (for example if you are creating it with [NSArray arrayWith...] it is autoreleased, you might not own it inside cellForRowAtIndexPath) and the hit object is properly initialized before giving it to the results array.

于 2013-05-17T07:51:16.080 回答
0

您的问题的答案在于错误消息:

无法识别的选择器发送到实例 0x1481c4。

接下来需要做的是通过 po 0x1481c4 打印该地址的值。看起来它实际上不是一个字符串,但您没有显示该代码。

于 2013-05-17T06:42:50.360 回答
0

这意味着hit = (SearchResult*)[results objectAtIndex:[indexPath row]];返回一个常量字符串而不是SearchResult对象

最好在hit从中获取值之前检查是否与 SearchResult 相同的类类型neighboringText

你可以尝试这样的事情:

if([hit isKindOfClass:[SearchResult Class]]){
  // do something with hit
}
else{
// different class
}
于 2013-05-17T06:45:47.437 回答