应用程序执行表委托方法时崩溃

0 objective-c uitableview ios

当我在aepub阅读器中运行搜索功能时,我的应用程序崩溃了.它以索引方法进入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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

tro*_*foe 5

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

hit = (SearchResult*)[results objectAtIndex:[indexPath row]];
Run Code Online (Sandbox Code Playgroud)

线索在异常文本中:

-[__NSCFConstantString neighboringText]: unrecognized selector sent to instance ...
  ^^^^^^^^^^^^^^^^^^^^                   ^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

任何数量的铸造SearchResult都不会改变这一点.

编辑:实际上,无论你在哪里看到演员,你都应该怀疑你正在处理的实际对象.如果您不确定,请检查isKindOfClass:.