2

我想要一个像这样的 NSPredicate :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(latitude - %@) < %@",coordsLatitude, kMaxLatLong];

我基本上想在 Core Data 中找到与userLocation.

  • latitude是每个 gps 实体的纬度属性。

  • coordsLatitude是 userLocation 的纬度。

  • kMaxLatLong设置为 1

我得到了EXC_BAD_ACCESS这条线。我认为这是由于我的谓词格式不正确,尤其是减法运算符。但是,我找不到任何说明如何使用 NSPredicates 和 NSExpressions 的减法。

4

1 回答 1

3

我会反过来做。

计算出最小和最大纬度,然后使用谓词查找介于两者之间的所有内容。

IE

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。

于 2013-02-02T20:32:07.103 回答