我有2个具有一对多关系的实体。 Category
代表我的部分标题,而SubSubCategory
代表我的行。
Category
{
name:string
subs<-->>SubCategory
}
SubCategory
{
name:string
numbervalue:NSNumber
}
目前,我可以使用
NSPredicate
根据名称过滤我的部分。NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@"anamevalue"];
现在,我想进一步过滤此谓词,以仅返回具有
Subcategory
和subvalue == 1
的类别。如何根据一对多子对象中的值过滤
Category
实体?我尝试过类似的方法,但是效果不好。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@ AND subs.numbervalue==%@",@"anamevalue",1];
最佳答案
我将做如下谓词:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ AND ANY subs.numbervalue == %@)", @"anamevalue", [NSNumber numberWithInt:1]];
ANY
是一个谓词修饰符,仅当subs集合中至少有一个对象的比较结果为true时,它才返回true。可以通过
SUBQUERY
完成相同的结果。希望能帮助到你。
关于ios - 基于一对多值的NSPredicate过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10313599/