Search code examples
iosobjective-cuitableviewnsmutablearraynsindexpath

[__NSCFString name]: unrecognized selector sent to instance


odd issue, my application has a UITableView with three sections, each populated by a separate array. The end-goal is to have the ability to move cells from section to section, so I wrote the 'logic' in the UITableViewController. Unfortunately, if I try to move a cell into an empty section, it crashes with the error [__NSCFString name]: unrecognized selector sent to instance. I cannot figure out the flaw in my code, so I thought I'd bring it here instead of spending more hours debugging in the wrong direction.

- (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];*/


    }



}

Notes:
* All of the NSMutableArrays are (nonatomic, retain).
* The exact line of failure is the [NSMutableArray insertObject:athlete.name atIndex: destinationIndexPath.row];.
* Athlete *athlete is an object with NSString properties, like athlete.name.


Solution

  • The error message is saying you are trying to call name method on NSString.

    This means you have an athlete that is NSString instead of Athlete.

    You can check the object type in debugger or use NSLog("%@ - %@", [athlete class], athlete) to log it.