Reputation: 21
I am creating a applications for teachers that will store Courses and students as well as keep track of grades. Right now it is set up in a data model that stores the courses in an array. The courses are objects that include an array of students and assignments. I have a tableview of all the courses that transitions to a tableview of students. In the courses detail view I have a tableview cell that transitions to a third table view for the assignments. My problem is occurring here. When I try to add an assignment to the tableview I am getting the error: 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x6b38620'..
Here is some code that I believe may contain the problem.
Course detail view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"AssignmentTable"]) {
AssignmentViewController *controller = segue.destinationViewController;
controller.checklist = checklistToEdit;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 2)
{
[self performSegueWithIdentifier:@"AssignmentTable" sender:checklistToEdit];
}
}
Assignment View controller:
- (void)editAssignmentViewController:(EditAssignmentViewController *)controller didFinishAddingItem:(Assignment *)item
{
int newRowIndex = [self.checklist.assignments count];
[self.checklist.assignments addObject:item];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
Assignment Detail View Controller:
- (IBAction)done
{
if (assnToEdit == nil) {
Assignment *item = [[Assignment alloc] init];
item.texta = self.textFielda.text;
[self.delegate editAssignmentViewController:self didFinishAddingItem:item];
} else {
self.assnToEdit.texta = self.textFielda.text;
[self.delegate editAssignmentViewControllerDidCancel:self];
}
}
Upvotes: 0
Views: 160
Reputation: 15597
The object you've stored in the assignments
property is an instance of NSArray
, which is immutable. You need that to be an instance of NSMutableArray
instead. That's the reason addObject:
is an unrecognized selector -- it's a method that exists in NSMutableArray
but not in NSArray
and your code is sending it to an instance of NSArray
.
Upvotes: 2