我想对 RLMResults
中包含的对象进行排序count
的实例的 RLMArray
那些对象的属性。
人.h:
@interface Person : RLMObject
@property RLMArray *children;
@end
SomeClass.m:
...
RLMResults *people = [Person allObjects];
RLMResults *sorted = [people sortedResultsUsingProperty:@"children.count" ascending:YES];
...
使用
- sortedResultsUsingProperty:
但是,如上所述不起作用,会出现错误:Cannot sort on 'people.count': sorting on key paths is not supported.
解决这个问题的最佳方法是什么?
最佳答案
到目前为止,您只能按实际属性排序。我们想在此处添加对您想要实现的目标的支持,请参阅 issue #1277以供引用。现在你需要介绍一个属性 childrenCount
,您将手动维护它并与 children
保持同步.或者,您可以将结果提取到 NSArray
并通过 Foundation 的 API 以类似的方式对其进行排序。
后一个提案可以像这里显示的那样实现:
RLMResults *people = [Person allObjects];
NSArray *peopleObjects = [people valueForKey:@"self"];
NSArray *sortedPeople [peopleObjects sortedArrayUsingComparator: ^(Person *a, Person *b) {
return a.children.count < b.children.count
}];
关于ios - 是否可以按 RLMArray 属性的计数对 RLMResults 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34150421/