为核心数据中的可转换属性编写 SortDescriptor?
我有一个实体标头,它有一个属性 NSDictionary ,该属性在模型中声明为可转换的。现在我不想对 fetchedRequestController 中的标头数组进行排序。传递 nil 或 null 对象会产生错误。请帮忙,很紧急。
让我从上次开始重新构建:如果我有一个实体,带有可转换的属性标题。我在生成的类中将 id 类型更改为 NSDictionary。我现在需要以实体.属性名称.key 的形式访问字典的键...我收到错误,因为此类不是键的键值编码投诉:(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 进行排序...
I have an entity header and it has a attribute NSDictionary wich was declared transformable in model. Now I want to not sort the array of headers in fetchedRequestController. And passing nil or null object gives error. Please help its urgent.
Let me reframe from the last time : if i have an entity, with a transformable attribute headers. I change the id type to NSDictionary in the generated class. I now need to access keys of the dictionary as entity.attribute name.key ... i get an error as this class is not key value coding complaint for the key :(key) ... what is a work around this problem.
// 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;
the above selector gives an error cause it can not sort the headersDictionary...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要在 Core Data 中存储 NSDictionary?简单来说,就是做错了。如果您想要类似字典的东西,只需在具有名称和值的一对多关系的另一端创建一个子对象。
如果您正确设计模型,那么您将不会遇到现在遇到的问题,您可以直接在 NSFetchRequest 中使用谓词进行过滤和排序描述符。
更新
仍然没有任何意义。我描述的设计与在可转换对象中包含字典相同,只是它在核心数据级别有用。在我描述的设计中,您不需要有固定的键。
重新思考你的设计,这个问题就变得无关紧要了。
Why are you storing a NSDictionary in Core Data? That is to put it simply, doing it wrong. If you want something like a dictionary just create a child object on the other end of a one to many relationship that has a name and a value.
If you design your model correctly then you will not have the problem you are running into now and you can use a predicate to filter and a sort descriptor in your NSFetchRequest directly.
Update
Still does not make any sense. The design I described is identical to having a dictionary inside a transformable object except that it is useful at the Core Data level. You do not need to have fixed keys in the design I described.
Rethink your design and this question becomes irrelevant.