I wanna extract from an array of SKSpriteNode only the elements that intersect with a predeterminated frame. I can do it by a for iteration:
for (SKSpriteNode* Object in Array) {
if (CGRectIntersectsRect(Frame,Object.frame)) {
//extraction code
}
}
However performace of this method seems to be poor,there's a way to do this operation in a faster way? I have tried something like this:
NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"CGRectIntersectsRect(Frame,SELF.frame)"];
NSArray *Results = [Array filteredArrayUsingPredicate:Predicate];
But this create the error "Unable to parse function name 'CGRectIntersectsRect' into supported selector (CGRectIntersectsRect)". What's wrong? Using a predicate instead a for will give me some gain in performance?
Since predicate parser cannot recognize a free-standing C function, you can make a predicate from a block:
NSPredicate *intersects = [NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) {
return CGRectIntersectsRect(Frame, obj.frame);
}];
NSArray *results = [Array filteredArrayUsingPredicate:intersects];
I am not sure about the performance gain in comparison to the loop, though, because the number of comparisons is going to remain unchanged.