4
votes

I have a CoreData Entity containing latitude and longitude coordinates. Just want to set a NSPredicate to get records between certain coordintes. This is what I got so far:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lat <= %f AND lat >= %f AND lon <= %f AND lon >= %f",minLat,maxLat,minLon,maxLon];

This predicate returns no result at all. Then I tried to search with BETWEEN condition from NSArray with [NSNumber numberWithFloat:] objects and got a crash with exception NSCFNumber contstantValue unrecognized selector sent to instance

Any suggestions would be much appreciated.

1
minLat,maxLat,minLon,maxLon=>maxLat,minLat,maxLon,minLon. Simple logic issue it seems. - Larme
Just to clarify, as I found out recently, BETWEEN cannot be used with an NSPredicate used with CoreData, and is the cause of your crash. - koen

1 Answers

5
votes

Change your code to

NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"lat <= %f AND lat >= %f AND lon <= %f AND lon >= %f",
maxLat, minLat, maxLon, minLon];