我有一个 UITableViewController,它显示了存储在核心数据中的用户定义的收藏夹列表。当表格视图加载时,托管对象被放置在一个可变数组中。要构造可变数组,我使用以下静态方法(在单独的类中):
+(NSMutableArray*)getAllFavorites{
NSManagedObjectContext *context = [[FavesDataModel sharedDataModel] mainContext];
NSFetchRequest *fetchReq = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortByIndex = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES];
[fetchReq setSortDescriptors:[NSArray arrayWithObject:sortByIndex]];
NSEntityDescription *entity = [NSEntityDescription entityForName:[Favorite entityName] inManagedObjectContext:context];
[fetchReq setEntity:entity];
NSError *err = nil;
return (NSMutableArray*)[context executeFetchRequest:fetchReq error:&err];
}
在我的表格视图中,我需要允许用户重新排序行。我已经验证了我最喜欢的托管对象的可变数组是完整的,但是,当我尝试删除给定索引处的对象时,会在运行时引发异常(-[_PFArray removeObjectAtIndex:]: unrecognized selector sent to instance) . 这是导致异常的代码:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
[self setEditing:YES animated:YES];
NSInteger rowIndex = fromIndexPath.row;
Favorite *fave = [allFavoriteObjects objectAtIndex:rowIndex];
[allFavoriteObjects removeObjectAtIndex:rowIndex]; // exception thrown here
[allFavoriteObjects insertObject:fave atIndex:toIndexPath.row];
}
有什么建议么?谢谢!