我想要一个像这样的 NSPredicate:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(latitude - %@) < %@",coordsLatitude, kMaxLatLong];
我基本上想在核心数据中找到距 userLocation
一定距离内的 GPS 坐标。
latitude
是每个 GPS 实体的纬度属性。coordsLatitude
是 userLocation 的纬度。kMaxLatLong
设置为 1
我通过此行获得了 EXC_BAD_ACCESS
。我认为这是由于我的谓词格式错误,特别是减法运算符。但是,我找不到任何说明如何使用 NSPredicates 或 NSExpressions 进行减法的内容。
最佳答案
我会反过来做。
计算出最小和最大纬度,然后使用谓词查找介于两者之间的所有内容。
即
float coordsLatitude = //whatever
float minLatitude = coordsLatitude - kMaxLatLong;
float maxLatitude = coordsLatitude + kMaxLatLong;
NSPredicate *minPredicate = [NSPredicate predicateWithFormat:@"latitude >= %f", minLatitude];
NSPredicate *maxPredicate = [NSPredicate predicateWithFormat:@"latitude <= %f", maxLatitude];
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubPredicates:@[minPredicate, maxPredicate]];
然后在提取请求中使用compoundPredicate。
关于objective-c - NSPredicate 中的减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666161/