我是NSPredicate的新手,如果这是一个新手问题,我很抱歉,但我已经做了调查,找不到答案。
我想通过函数而不是属性来过滤自定义对象的数组。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ LIKE %@", [times objectAtIndex:x] ,[unit realTime]];
NSArray *filtered = [objectArray filteredArrayUsingPredicate:predicate];objectArray仅包含unit对象。我想通过array by [unit realTime]方法结果中的每个对象来过滤数组。基本上,我希望有过滤的数组,其中[times objectAtIndex:x] == [unit realTime]。这有可能做到吗?
发布于 2013-01-18 17:15:10
您可以将realTime作为@property() NSDate *realTime;添加到Unit.h文件中,然后实现realTime方法,就像您已经做的那样。这样,realTime就是类中的一个参数,您可以使用自定义实现覆盖getter方法。在这种情况下,它不应该显示undeclared identifier错误。
完成后,将谓词语句更改为,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", unit.realTime, [times objectAtIndex:x]];
NSArray *filtered = [objectArray filteredArrayUsingPredicate:predicate];对于unit.realTime,您应该使用%K,而不是%@。
https://stackoverflow.com/questions/14395071
复制相似问题