问题描述
我有一个具有一对多关系的Core Data模型,例如:
I have a Core Data model with a one-to-many relationship e.g.:
@interface Person : NSManagedObect
@property (nonatomic, retain) NSSet *children;
@end
我想创建一个谓词,该谓词只给我至少有一个孩子的 Person
:
I want to create a predicate which only gives me the Person
s that have at least one child:
我尝试过: [NSPredicate predicateWithFormat:@"person.children.count> 0"]
但是我得到了 NSPredicate一对多密钥不允许
.
推荐答案
好,我发现了一些文档关于 NSPredicate
集合查询的 realm.io 网站上,其答案为:
Ok, I found some documentation on the realm.io site about NSPredicate
collection queries which has the answer:
您必须使用 @count
,而不仅仅是 count
:
You have to use @count
instead of just count
:
因此: [NSPredicate predicateWithFormat:@"person.children.@ count> 0"]
可惜苹果没有自己记录(至少不是我能找到的).
Pity that Apple doesn't document this themselves (at least not that I could find).
这篇关于NSPredicate表达式可根据一对多关系的数量进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!