使用谓词按函数过滤对象数组

时间:2013-01-18 08:34:36

标签: ios objective-c

我对NSPredicate很新,对不起,如果这是新手问题,但是我已经完成了我的研究而无法找到答案。

我想按功能过滤自定义对象数组,而不是属性。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ LIKE %@", [times objectAtIndex:x] ,[unit realTime]];
NSArray *filtered  = [objectArray filteredArrayUsingPredicate:predicate];

objectArray仅包含unit个对象。我想通过[unit realTime]方法结果按Array中的每个对象过滤数组。基本上我想要过滤数组[times objectAtIndex:x] == [unit realTime]。这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以在Unit.h文件中添加realTime作为@property() NSDate *realTime;,然后像您已经完成的那样实现realTime方法。那样realTime是类中的一个参数,你用你的自定义实现覆盖了getter方法。在这种情况下,它不应显示undeclared identifier错误。

完成后,将谓词语句更改为

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", unit.realTime, [times objectAtIndex:x]];
NSArray *filtered  = [objectArray filteredArrayUsingPredicate:predicate];

您应%K使用unit.realTime而非%@