An NSPredicate that can recursively traverse an object graph?

拜拜、爱过 提交于 2019-12-05 19:26:40

Its been awhile since I asked this question, and I think I went in another direction with what i was doing, but there are some possibilities i realize now to solve what i wanted at the time:

  • Have the visible property method behave recursively instead of making the format predicate do that. This could be accomplished like so:
- (BOOL) isVisible {
  return visible && [parent isVisible];
}

//...
id filtered = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"visible == YES"]];
  • Use block predicates instead of format predicates to do the recursive traversal:
[array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {

    id obj = evaluatedObject;
    while (obj) {
      if (![obj isVisible]) return NO;
      obj = [obj parent];
    }
    return YES;
}]];

Or a combination of the two (which would be the most robust and readable I think).

I'm not sure what you're looking to do is possible with a simple, single predicate. If it's a tree and you're using a predicate to get the nodes, you'd want and write a method that will traverse upwards and return a BOOL indicating whether it should be removed or not.

Then, just get your nodes and put them in an NSMutableArray and do a

for (int i = 0; i < [results count]; i++)
{
    if ([self shouldBeRemoved:[results objectAtIndex:i]])
    {
        [results removeObjectAtIndex:i];
        i--;
    }
}

Your shouldBeRemoved: method should be pretty a straightforward recursive method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!