首页 > 解决方案 > CoreData NSPredicate 用于 Swift Core Data 中的算术计算

问题描述

对于以下问题,我如何使用 NSpredicate 在 swift CoreData 中编写 Sql 查询,我想根据位置模型中的某些字段查找范围内的位置:

下面的字段是在查询之前预先计算好的。

    let CUR_cos_lat = cos(currenLocation.latitude * .pi / 180)
    let CUR_sin_lat = sin(currenLocation.latitude * .pi / 180)
    let CUR_cos_lng = cos(currenLocation.longitude * .pi / 180)
    let CUR_sin_lng = sin(currenLocation.longitude * .pi / 180)
    let cos_allowed_distance = cos(2.0 / 6371)

我想使用 NSPredicate 在 Swift CoreData 中编写如下查询:

SELECT * FROM position WHERE CUR_sin_lat * sin_lat + CUR_cos_lat * cos_lat * (cos_lng* 
CUR_cos_lng + sin_lng * CUR_sin_lng) > cos_allowed_distance;

上述查询中的以下字段已在 CoreData 模型中可用,

  1. sin_lat
  2. cos_lat
  3. cos_lng
  4. sin_lng
  5. cos_allowed_distance

这是我尝试过但没有返回任何位置,我的表达有什么问题

 let predicateS = NSPredicate(format: "(%f * trignometry.sinLat + %f) * trignometry.cosLat * (trignometry.cosLng * %f + trignometry.sinLng * %f ) > %f",CUR_sin_lat,CUR_cos_lat,CUR_cos_lng,CUR_sin_lng,cos_allowed_distance)

标签: iosswiftcore-datacore-locationnspredicate

解决方案


推荐阅读