为核心数据中的可转换属性编写 SortDescriptor?

发布于 2024-09-11 07:26:19 字数 1318 浏览 7 评论 0原文

我有一个实体标头,它有一个属性 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2024-09-18 07:26:19

为什么要在 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

ohh thankYou ... the problem is the dictionaries i am getting from the web does not have a fixed Keys structure .. so I have to get the dictionary as it is as a transformable attribute

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文