有没有办法做到这一点?我有一组要从另一组中排除的项目。我知道我可以遍历我的集合中的每个项目,如果它不在另一个集合中,则只将它添加到我的过滤集合中,但如果我可以使用谓词会很好。
要排除的项目集合不是直接相同类型的对象集合;它是一组字符串;如果其中一个属性与该字符串匹配,我想从我的第一组中排除任何内容....换句话说:
NSMutableArray *filteredArray = [NSMutableArray arrayWithCapacity:self.questionChoices.count];
BOOL found;
for (QuestionChoice *questionChoice in self.questionChoices)
{
found = NO;
for (Answer *answer in self.answers)
{
if ([answer.units isEqualToString:questionChoice.code])
{
found = YES;
break;
}
}
if (!found)
[filteredArray addObject:questionChoice];
}
这可以用谓词代替吗?