【问题标题】:[__NSCFString name]: unrecognized selector sent to instance[__NSCFString name]:发送到实例的无法识别的选择器
【发布时间】:2015-01-28 04:53:11
【问题描述】:

奇怪的问题,我的应用程序有一个包含三个部分的 UITableView,每个部分由一个单独的数组填充。最终目标是能够将单元格从一个部分移动到另一个部分,所以我在 UITableViewController 中编写了“逻辑”。不幸的是,如果我尝试将一个单元格移动到一个空白部分,它会因错误[__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

【问题讨论】:

  • 所以athleteNSString 而不是Athlete
  • @BryanChen 运动员是一个具有“已分配属性”的 NSObject,例如,运动员名称是 NSString,运动员不是 NSString。
  • 这是您所期望的,但显然不会发生,否则您将看不到该错误。
  • @BryanChen,这很奇怪,我在第一个“if”块中设置了一个检查(如果运动员!= nil)。可能发生的情况是运动员对象没有属性,但我看不出这是怎么发生的。
  • 只是NSLog("%@ - %@", [athlete class], athlete)

标签: ios objective-c uitableview nsmutablearray nsindexpath


【解决方案1】:

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

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

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

【讨论】:

  • 你这个人!度过美好的一天/晚上/晚上/早上/下午。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多