Swift 3核心数据NSExpression总是返回0

时间:2017-09-03 22:43:15

标签: ios swift core-data

我正在尝试获取Core Data中托管对象的属性的总和,但似乎无论我做什么我得到0。

我试图获取所有Opportunity对象上的estimatedValue属性的总和。 estimatedValue属性是Core Data中的小数。

这是我当前的代码版本:

[{
    sumOfEstimatedValue = 0;
}]

上述代码的结果是:

[<Opportunity: 0x60800009dd80> (entity: Opportunity; id: 0x60800002d8c0 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C17> ; data: {
    company = nil;
    contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
    createdDate = "2017-09-03 22:40:00 +0000";
    estimatedValue = "42.41";
    opportunityDescription = nil;
    projectedCloseDate = nil;
    stage = "0x60800002d860 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C16>";
}), <Opportunity: 0x60800009dc90> (entity: Opportunity; id: 0x60800002d380 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C15> ; data: {
    company = nil;
    contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
    createdDate = "2017-09-03 22:40:00 +0000";
    estimatedValue = "42.41";
    opportunityDescription = nil;
    projectedCloseDate = nil;
    stage = "0x60800002d440 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C14>";
}), <Opportunity: 0x60800009d9c0> (entity: Opportunity; id: 0x60800002b8e0 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C9> ; data: {
    company = nil;
    contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
    createdDate = "2017-09-03 22:40:00 +0000";
    estimatedValue = "42.41";
    opportunityDescription = nil;
    projectedCloseDate = nil;
    stage = "0x60800002aaa0 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C8>";
}), <Opportunity: 0x60800009dab0> (entity: Opportunity; id: 0x60800002c580 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C11> ; data: {
    company = nil;
    contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
    createdDate = "2017-09-03 22:40:00 +0000";
    estimatedValue = "42.41";
    opportunityDescription = nil;
    projectedCloseDate = nil;
    stage = "0x60800002d3e0 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C10>";
}), <Opportunity: 0x60800009dba0> (entity: Opportunity; id: 0x60800002d140 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C13> ; data: {
    company = nil;
    contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
    createdDate = "2017-09-03 22:40:00 +0000";
    estimatedValue = "42.41";
    opportunityDescription = nil;
    projectedCloseDate = nil;
    stage = "0x60800002c140 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C12>";
}), <Opportunity: 0x60800009d3d0> (entity: Opportunity; id: 0x60800002b3a0 <x-coredata:///Opportunity/tB6E74585-FDA0-4061-89CC-D89EEB25156C7> ; data: {
    company = nil;
    contact = "0x608000028d40 <x-coredata:///Contact/tB6E74585-FDA0-4061-89CC-D89EEB25156C2>";
    createdDate = "2017-09-03 22:40:00 +0000";
    estimatedValue = "42.41";
    opportunityDescription = nil;
    projectedCloseDate = nil;
    stage = "0x60800002abe0 <x-coredata:///Stage/tB6E74585-FDA0-4061-89CC-D89EEB25156C6>";
})]

以下是打印应用中保存的商机的信息:

{{1}}

感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

核心数据不支持您尝试执行的表达。

来自Apple Docs:

  

Core Data不支持聚合表达式。

来源:https://developer.apple.com/documentation/foundation/nsexpression

相反,您应该在执行提取后对所有实体求和。

答案 1 :(得分:0)

原始问题中的代码现在正在运行。我不太确定出了什么问题,但是从我的设备上擦除应用数据并使用完全相同的数据填充它来固定它。