objective-c - CGPoint 的 NSPredicate 过滤器类

标签 objective-c nspredicate cgpoint

我有那个课:

@interface Field : NSObject
@property CGPoint fieldCoordinates;
@property CGPoint ballCoordinates;
@end

然后我尝试过滤此类对象的 NSArray :

NSPredicate * y1 = [NSPredicate predicateWithFormat:@"ballCoordinates.y >= %@",location.y];
NSArray * filtered = [self.fieldsArray filteredArrayUsingPredicate:y1];

但出现错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key y.'

NSPredicate 使用 CGPoint 过滤有问题吗?

最佳答案

是的,NSPredicateCGPoint 有问题,因为它是一个 struct,而不是一个键值兼容的 Objective-C 类。您可以改为使用 block 编写谓词,如下所示:

NSPredicate * y1 = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) {
    return ((CGPoint)[obj ballCoordinates]).y >= location.y;
}];

关于objective-c - CGPoint 的 NSPredicate 过滤器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867897/

相关文章:

iphone - 带有 UIPageControl 的 GMGridView

ios - XCode & Instruments,如何清除进程列表

objective-c - 如何识别同一张脸?

objective-c - 通过另一个托管对象数组过滤一个托管对象数组

ios - 保存坐标或更新帧

ios - UIView 作为 maskView- 由约束常量移动的原点

iphone - 在 didSelectRowAtIndexPath 中的单元格上找到一个确切的 UIImageView

ios - eventsMatchingPredicate 在跨越多天的事件中找不到最后日期

ios - 在 Xcode Beta 6 之后快速使用 NSPredicate 崩溃

ios - 如何在 Swift 中比较两点