核心数据推导表达式密钥路径使用运算符作为中间组件 [英] core data derivation expression key path uses an operator as an intermediate component

查看:38
本文介绍了核心数据推导表达式密钥路径使用运算符作为中间组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为多对多关系属性的总和编写推导表达式.

I'm trying to write the derivation expression for the sum of a to many relationship attribute.

我有一个项目和一个组,该项目有一个价格和总价(金额*价格).我想写一个表达式,表示该组的总价格,作为其组成部分的总和.

I have an item and a group, the item has a price and total price (amount * price). I want to write an expression for the total price for the group as the sum of its components.

建造时出现错误

错误:配置错误的属性:LAEItemGroup.totalPrice密钥路径"items.@ sum.totalPrice"使用运算符作为中间变量组件

error: Misconfigured Property: LAEItemGroup.totalPrice key path "items.@sum.totalPrice" uses an operator as an intermediate component

根据文档

according to the documentation and the WWDC 2019 Making Apps with Core Data it should be possible to get the sum on a to many relationship.

有人可以帮助我找到正确的语法或方法吗?

Could someone please help me find the correct syntax or way to do so.

作为解决方案,我试图编写一个在该类中正常工作的var

As a work around I tried to write a var that worked in that class as so

@objc
public var totalPrice: Double {
    value(forKeyPath: "items.@sum.totalPrice") as? Double ?? 0
}

那为什么KeyPath值可以工作却不能在模型编辑器中工作呢?

so why the KeyPath value works but not in the model editor?

推荐答案

我现在有机会进行检查.看来,模型编辑器使用的格式是使聚合运算符位于表达式的末尾(如您所指出的,它不同于其他表达式中使用的格式):

I've now had an opportunity to check. It seems the format used by the model editor is for the aggregate operator to be at the end of the expression (which as you point out, is different from the format used in other expressions):

items.totalPrice.@sum

这篇关于核心数据推导表达式密钥路径使用运算符作为中间组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆