我有那个课:
@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 过滤有问题吗?
最佳答案
是的,NSPredicate
与 CGPoint
有问题,因为它是一个 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/