user3657809
user3657809

Reputation: 3

UITableViewController error

I'm new to xCode and having problems with working out what this error is saying:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CharacterObject copyWithZone:]: unrecognized selector sent to instance 0x92606b0'

When I compile no errors are reported. The error is a "SIGABIT" and appears on this line:

cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

Here is the code I have. The "CharacterObject" is an NSObject file.

@interface CharacterTableViewController ()
{
    NSMutableArray *mycharacterArray;
    NSMutableArray *myguestArray;
    NSString *mainCharacters;
    NSString *guestCharacters;
}


    - (void)viewDidLoad
{

    [super viewDidLoad];
    mysections =[[NSArray alloc]init];
    mycharacterArray = [[NSMutableArray alloc]init];
    myguestArray = [[NSMutableArray alloc]init];
    CharacterObject *myobject = [[CharacterObject alloc]init];
    guestObject *myguests = [[guestObject alloc]init];
    myheaders = [[NSArray alloc]initWithObjects:@"Main Characters", @"Guest Characters", nil];

    mysections = [NSArray arrayWithObjects:mycharacterArray, myguestArray, nil];

    myobject.charName = @"Joe Bloggs";
    myobject.charPic = @"RB";
    myobject.charNotes = @"Hello World";
    [mycharacterArray addObject:myobject];

    myguests = [[guestObject alloc]init];
    myguests.charName = @"Jonny Boy";
    myguests.charPic = @"JB";
    myguests.charNotes = @"Jonny Boy was here";
    [myguestArray addObject:myguests];

    mysections = [NSArray arrayWithObjects:mycharacterArray, myguestArray, nil];
}

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [mysections count];
}

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [myheaders objectAtIndex:section];
}


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [[mysections objectAtIndex:section]count];
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"mycell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

    @end

Thanks in advance for any help. Any questions please let me know and I'll try and answer them.

Phil W.

Upvotes: 0

Views: 66

Answers (1)

KerrM
KerrM

Reputation: 5240

That error means that your object of class "CharacterObject" received a call to the method "copyWithZone" and it doesn't implement that method.

When the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath gets called it tries to populate the cell, and in the line containing:

cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

You are trying to cast a CharacterObject to an NSString (i.e. the first time cellForRowAtIndexPath runs it uses indexPath (section 0, row 0) - which in turn means that in the line:

cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

The first part:

[mysections objectAtIndex:indexPath.section]

Will return an array containing your myobject, and the second part objectAtIndex:indexPath.row] will get the object at index 0 (the actual myobject).

To fix this, change:

cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

to:

cell.textLabel.text = ((CharacterObject*)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]).charName;

which will get the property charName from a CharacterObject. Note that this will cast your guestObject class also, which will be a problem. You should either test for the class type of the object returned by [[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] or have both objects inherit from an class that implements the common methods/properties you need .

Upvotes: 1

Related Questions