[__NSCFString 名称]:发送到实例的无法识别的选择器

[__NSCFString name]: unrecognized selector sent to instance

奇怪的问题,我的应用程序有一个包含三个部分的 UITableView,每个部分由一个单独的数组填充。最终目标是能够将单元格从一个部分移动到另一个部分,因此我在 UITableViewController 中编写了 'logic'。不幸的是,如果我尝试将单元格移动到空白部分,它会崩溃并显示错误 [__NSCFString name]: unrecognized selector sent to instance。我无法找出我的代码中的缺陷,所以我想我会把它带到这里,而不是花更多的时间在错误的方向上进行调试。

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {




    if (sourceIndexPath.section == 0) {
     Athlete *athlete = players[sourceIndexPath.row];
        NSLog(@"sta count: %lu", (unsigned long)[players count]);
        if (athlete != nil) {

        if (destinationIndexPath.section == 1) {
                [players removeObjectAtIndex:sourceIndexPath.row];
                 [benched insertObject:athlete.name atIndex:destinationIndexPath.row];
        }else if (destinationIndexPath.section == 2){

            [players removeObjectAtIndex:sourceIndexPath.row];

                 [reserved insertObject:athlete.name atIndex:destinationIndexPath.row];
        }
        }
    } else if (sourceIndexPath.section == 1) {
    Athlete *athlete = benched[sourceIndexPath.row];
           NSLog(@"Benched count: %lu", (unsigned long)[benched count]);
        if (destinationIndexPath.section == 0) {

            [benched removeObjectAtIndex:sourceIndexPath.row];

            [players insertObject:athlete.name atIndex:destinationIndexPath.row];
        }else if (destinationIndexPath.section == 2){

            [benched removeObjectAtIndex:sourceIndexPath.row];

            [reserved insertObject:athlete.name atIndex:destinationIndexPath.row];
        }
    }else if (sourceIndexPath.section == 2) {
          Athlete *athlete = reserved[sourceIndexPath.row];
           NSLog(@"Res count: %lu", (unsigned long)[reserved count]);
        NSLog(@"From starter");
        if (destinationIndexPath.section == 1) {
            NSLog(@"To bench");
            [reserved removeObjectAtIndex:sourceIndexPath.row];
            [benched insertObject:athlete.name atIndex:destinationIndexPath.row];
        }else if (destinationIndexPath.section == 0){
            NSLog(@"To reserves");
            [reserved removeObjectAtIndex:sourceIndexPath.row];

            [players insertObject:athlete.name atIndex:destinationIndexPath.row];

    }





   /* [players removeObjectAtIndex:sourceIndexPath.row];
    [self.tableView reloadData];
    [players insertObject:athlete.name atIndex:destinationIndexPath.row];*/


    }



}

备注:
* 所有 NSMutableArrays 都是 (nonatomic, retain).
* 确切的失败行是 [NSMutableArray insertObject:athlete.name atIndex: destinationIndexPath.row];.
* Athlete *athlete 是具有 NSString 属性的对象,如 athlete.name.

错误消息说您正试图在 NSString 上调用 name 方法。

这意味着您有一个 athlete,即 NSString 而不是 Athlete

您可以在调试器中检查对象类型或使用 NSLog("%@ - %@", [athlete class], athlete) 记录它。