如何使用NSPredicate过滤另一个类属性?

时间:2016-04-21 11:49:07

标签: ios objective-c nspredicate

我有以下课程

EntityOne
- Property1 : string
- Property2 : int
- Property3 : Array of EntityTwo

EntityTwo
- Property1 : string
- Property2 : int

我想写一个NSPredicate,当我在一个包含EntityTwoObj.property1个对象的数组上执行它时会对EntityOne进行过滤。

有可能吗?

我试过以下:

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:
@"Property1 CONTAINS[cd] %@ 
OR Property2 CONTAINS[cd] %@ 
OR Property3.Property1 CONTAINS[cd] %@", 
newString, newString, newString];

但它没有用。

如果我删除了最后一个过滤器,那么它的工作正常。

1 个答案:

答案 0 :(得分:0)

感谢@Larm,我可以通过在谓词中添加ANY运算符来解决它。

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:
@"Property1 CONTAINS[cd] %@ 
OR Property2 CONTAINS[cd] %@ 
OR ANY Property3.Property1 CONTAINS[cd] %@", 
newString, newString, newString];