gpt4 book ai didi

ios - NSSortDescriptor 与关系

转载 作者:行者123 更新时间:2023-11-28 19:15:23 24 4
gpt4 key购买 nike

我的 CoreData 模型中有 3 个实体

编队具有与 Sections 实体的 sections 关系以及与 Chapters 实体的 chapters 关系。

所以A Formation contains 1 or n sections contains 1 or n chapter

在我的实体部分中,有一个 sortNB 属性(一种 id),所以我想在我的请求中对我的部分进行排序。

我试过:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Formations" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];


NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"sections.sortNB" ascending:YES];
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"sections", nil]];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];


NSArray *forms = [[_managedObjectContext executeFetchRequest:request error:nil]mutableCopy];

但出现错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

我该如何继续?

我想在我的 Chapters 实体中插入相同类型的数字来对其进行排序,但首先,让我们为 Sections 解决它:p

谢谢

最佳答案

好的,所以我不是在我的请求中而是在使用之后对我的对象进行排序

NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"sortNB" ascending:YES]];

NSArray *sortedRecipes = [[[formation sections] allObjects] sortedArrayUsingDescriptors:sortDescriptors];

而且有效。

感谢您的帮助。

关于ios - NSSortDescriptor 与关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12268157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com