我有一个包含 CGPoints
的 NSArray
。我想通过筛选来仅包括在矩形内的点。
如何制定一个 NSPredicate
,使得每个点都满足以下条件:
CGRectContainsPoint(windowRect, point);
目前的代码如下:
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];
NSArray* filteredPoints = [points objectsAtIndexes:[points indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){ return CGRectContainsPoint(windowRect, [obj CGPointValue]); }]];
- Ken ThomasesNSPredicate
……”。 :) - Ken Thomases