0

我有一个实体头,它有一个属性 NSDictionary ,它在模型中被声明为可转换的。现在我不想对 fetchedRequestController 中的标头数组进行排序。并且传递 nil 或 null 对象会产生错误。请帮助其紧急。

让我从上次重新开始:如果我有一个实体,它具有可转换的属性标题。我在生成的类中将 id 类型更改为 NSDictionary。我现在需要以 entity.attribute name.key 的形式访问字典的键...我收到一个错误,因为此类不是键的键值编码投诉:(键)...解决此问题的方法是什么。

// Create and configure a fetch request with the Book entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"headers" inManagedObjectContext:self.tableCellGenericMoc]; [fetchRequest setEntity:entity]; // Create the sort descriptors array. NSSortDescriptor *headersDescriptor = [[NSSortDescriptor alloc] initWithKey:@"headersDictionary" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:headersDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; // Create and initialize the fetch results controller. NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.tableCellGenericMoc sectionNameKeyPath:@"headresDictionary" cacheName:@"Root"]; self.fetchedResultsController = aFetchedResultsController; fetchedResultsController.delegate = self;

上面的选择器给出了一个错误,因为它不能对 headersDictionary 进行排序......

4

1 回答 1

1

为什么要将 NSDictionary 存储在 Core Data 中?简单地说,就是做错了。如果您想要字典之类的东西,只需在具有名称和值的一对多关系的另一端创建一个子对象。

如果您正确设计模型,那么您将不会遇到现在遇到的问题,您可以直接在 NSFetchRequest 中使用谓词进行过滤和排序描述符。

更新

哦,谢谢你......问题是我从网上得到的字典没有固定的键结构......所以我必须得到字典,因为它是一个可转换的属性

仍然没有任何意义。我描述的设计与在可转换对象中包含字典相同,只是它在核心数据级别很有用。在我描述的设计中,您不需要使用固定键。

重新考虑你的设计,这个问题就变得无关紧要了。

于 2010-07-28T01:22:06.097 回答