ios - 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 模型中可用,
- sin_lat
- cos_lat
- cos_lng
- sin_lng
- 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)
解决方案
推荐阅读
- python - 读取以特定字符串 opencv - python 开头的图像
- eclipse - 当我想访问我使用 Eclipse 在 azure 中发布的应用程序时收到错误 HTTP 404
- python-3.x - Scrapy CrawlSpider:拒绝子域不起作用,为什么?
- c# - 查询“QueryName”的加载操作失败。远程服务器返回错误:未找到
- ruby - 如何在单个语句/检查中替换嵌套哈希的多个 nil 检查?
- resolve - 解析失败:multidex 打开文件 解析失败:运行时打开文件 解析失败:common 打开文件
- nuget - 如何弃用 nuget 包
- sitefinity - 我可以将 Sitefinity 数据库模式设置为 dbo 以外的模式吗?
- google-cloud-platform - Google Cloud HTTP 负载均衡器运行状况检查在没有外部 IP 的情况下失败
- mysql - 查询 HAVING MAX() 未按预期工作