我正在为
@SectionedFetchRequest
订购系统而苦苦挣扎。
我有一个具有 2 个属性的
Word
对象:
nameLanguageIdentifier
表示语言代码,如 it, fr, de, en, ...name
就是像“船”这样的词在我的列表中,我希望我的话按
name
在按 nameLanguageIdentifier
排序的部分中排序。这是一个小例子:
等等……
我写了这个获取请求:
@SectionedFetchRequest<String?, Word>(sectionIdentifier: \.nameLanguageIdentifier, sortDescriptors: [SortDescriptor(\.name)])
var words: SectionedFetchResults<String?, Word>
这是我的视图主体:
List(words) { section in
Section(header: Text(section.id)) {
ForEach(section) { word in
Text(word.name)
}
}
}
问题是我得到了一个完全随机的订单。例如,我可以在英语部分包含法语单词。 当我使用
nameLanguageIdentifier
作为部分标识符和排序描述符时,我摆脱了这种行为。但在这种情况下,我的话在自己的部分不再按名称排序。
我真的希望有人能帮助我,非常感谢。
正如@joakim 提到的那样,您必须指定要使用的所有排序描述符,不仅是用于将数据划分为部分的描述符,还包括用于您的部分的描述符。
对于我的例子,
[SortDescriptor(\.nameLanguageIdentifier), SortDescriptor(\.name)]
做这个把戏。