objective-c - NSPredicate 中的减法

标签 objective-c nspredicate

我想要一个像这样的 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/

相关文章:

iphone - Instagram 或 Hipstamatic 如何进行照片滤镜?

objective-c - 在 Objective C 中使用 nspredicate 时如何解决空间问题?

ios - 具有多个条件的 UITableView filteredArrayUsingPredicate?

ios - 通过过滤另一个 NSDictionary 得到一个 NSDictionary

iOS:错误 - 架构 x86_64 的 23 个重复符号

objective-c - sqlite3_prepare_v2 正在获取 SQLITE_ERROR

objective-c - 未调用 NSNotificationCenter 选择器

ios - 我可以设置 transient 属性来获取吗?

objective-c - 字符串包含数组中的任何条目

swift - 如何按日期 Swift NSPredicates 过滤 Realm 列表