CoreData:找到计算属性的最小值
假设我有一个 CoreData 实体“Point”,具有两个属性 x 和 y(均为 NSNumber)。
NSPredicate 需要是什么样子才能让我找到最接近 a、b 的点? for distance = sqrt((xa)(xa)+(yb)(yb))
虽然我可以定义一个瞬态属性来计算到预定义点的距离,但我不知道如何以编程方式在启动 fetchrequest 时更改该点。
任何帮助将不胜感激。
Say I have a CoreData entity "Point" with two properties x and y (both NSNumber).
How would a NSPredicate need to look like to let me find the closest Point to say a,b?
for distance = sqrt((x-a)(x-a)+(y-b)(y-b))
While I could define a transient property that calculates a distance to a predefined point I can't see how I could programmatically change that point when launching a fetchrequest.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,不支持在
NSPredicate
中进行算术运算。只能使用比较运算符。消息变得更糟。您不能在NSFetchRequest
中使用瞬态属性。只能使用持久属性。如果您确实需要这种级别的控制来获取对象,Core Data 可能无法满足您的需求。Sorry, but doing arithmetic in an
NSPredicate
isn't supported. Only comparison operators can be used. And the news gets worse. You can't use a transient property in anNSFetchRequest
. Only persistent properties can be used. If you really need this level of control in fetching your objects, Core Data may not be able to accommodate you.