Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate filter class by CGPoint

I have that class :

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

and i Try filter NSArray of object of this class :

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

but get error :

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

NSPredicate have problem with filter by CGPoint ?

like image 432
netmajor Avatar asked Sep 02 '26 08:09

netmajor


2 Answers

Yes, NSPredicate has problems with CGPoint, because it is a struct, not a key-value compliant Objective-C class. You can write a predicate with a block instead, like this:

NSPredicate * y1 = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) {
    return ((CGPoint)[obj ballCoordinates]).y >= location.y;
}];
like image 54
Sergey Kalinichenko Avatar answered Sep 04 '26 19:09

Sergey Kalinichenko


CGPoint is not an Object it's a plain old C struct.
You could walk around that by making a readonly property on your Field class that look like this. @property (nonatomic, readonly) CGFloat yBallCoordinates;
- (CGFloat)yBallCoordinates { return self.ballCoordinates.y; }


Edit
The block approach pointed by dasblinkenlight is a better solution.
Because it will not involve the necessity of declaring property for every thing you want to predicate on. Which will be more flexible.

like image 34
Vincent Bernier Avatar answered Sep 04 '26 19:09

Vincent Bernier