I have an NSArray with CGPoints. I’d like to filter this array by only including points within a rect.
How can I formulate an NSPredicate such that each point satisfies this predicate:
CGRectContainsPoint(windowRect, point);
Here’s the code so far:
NSArray *points = [NSArray arrayWithObjects: [NSValue valueWithCGPoint:pointAtYZero] ,
[NSValue valueWithCGPoint:pointAtYHeight],
[NSValue valueWithCGPoint:pointAtXZero],
[NSValue valueWithCGPoint:pointAtXWidth],
nil];
NSPredicate *inWindowPredicate = [NSPredicate predicateWithFormat:@"CGRectContainsPoint(windowRect, [point CGPointValue])"];
NSArray *filteredPoints = [points filteredArrayUsingPredicate:inWindowPredicate];
You cannot do this with the predicate format syntax, but you can use a block:
Note that it’s not possible to use block-based predicates for Core Data fetch requests.