首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用predicate by function过滤对象数组

使用predicate by function过滤对象数组
EN

Stack Overflow用户
提问于 2013-01-18 16:34:36
回答 1查看 1.2K关注 0票数 1

我是NSPredicate的新手,如果这是一个新手问题,我很抱歉,但我已经做了调查,找不到答案。

我想通过函数而不是属性来过滤自定义对象的数组。

代码语言:javascript
复制
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]。这有可能做到吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-18 17:15:10

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

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

代码语言:javascript
复制
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", unit.realTime, [times objectAtIndex:x]];
NSArray *filtered  = [objectArray filteredArrayUsingPredicate:predicate];

对于unit.realTime,您应该使用%K,而不是%@

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14395071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档