我有2个班级,分别是品牌和产品。品牌具有以下属性:产品的名称和关系(一对多)产品;而产品具有属性:品牌的名称和关系(一对一)品牌。
以下是错误发生的方式:
Brand1和Brand2有2个品牌。当我将P1,P2 ...保存到Brand1时,效果很好。但如果我尝试将新产品保存到Brand2,则会显示错误:
-[Brand compare:]: unrecognized selector sent to instance
以下是添加新产品的代码:
- (void)addProduct:(NSString *)name brandOfProduct:(Brand *)brand
{
Product *newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:self.coreDataManager.managedObjectContext];
newProduct.name = name;
[brand addProductsObject:newProduct];
NSError *error;
[self.coreDataManager.managedObjectContext save:&error];
}
'品牌'这里指的是在选择器视图中选择的一个用户
Brand *brand = [[self.fetchedResultsController fetchedObjects] objectAtIndex:row];
更新1:错误显示调用[self.coreDataManager.managedObjectContext save:&error];
时将新产品保存到Brand2。
错误消息:
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[Brand compare:]: unrecognized selector sent to instance 0x1092d97a0 with userInfo (null)
更新2:我发现导致此错误的位置:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}