NSPredicate表达式可根据一对多关系的数量进行过滤 [英] NSPredicate expression to filter on count of a to-many relationship

查看:54
本文介绍了NSPredicate表达式可根据一对多关系的数量进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有一对多关系的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 Persons 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表达式可根据一对多关系的数量进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆