编辑:如果有人遇到同样的问题,索引路径不是问题所在。 问题是我没有从主视图控制器(我的tableview所在的位置)发送fetchedResultsController和managedObjectContext到我的详细视图控制器(我正在更新数据), 因此,它试图删除某个索引处的数据,但它没有数据。
所以我收到了错误
由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' -deleteObject:需要非零参数'
但问题是,它不是 nil 。这是方法:
- (void) updateMethod {
// NSIndexPath *path = [NSIndexPath indexPathWithIndex:self.indexToDelete];
//NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.indexToDelete inSection:0];
//NSLog(@"path = %@", indexPath);
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:self.indexToDelete]];
ContactDetails *contactDetails = [NSEntityDescription insertNewObjectForEntityForName:[entity name]inManagedObjectContext:context];
contactDetails.address = self.txtAddress.text;
contactDetails.firstName = self.txtFirstName.text;
contactDetails.lastName = self.txtLastName.text;
contactDetails.image = self.txtPicture.text;
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
这是self.indexToDelete:
的输出{length = 2,path = 0 - 1}
我错过了一些简单的东西吗?