gpt4 book ai didi

ios - Swift 3 核心数据 NSExpression 始终返回 0

转载 作者:行者123 更新时间:2023-11-30 12:11:14 26 4
gpt4 key购买 nike

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

我试图获取所有机会对象上的estimatedValue属性总和的对象。 estimatedValue 属性是核心数据中的小数。

这是我当前版本的代码:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Opportunity")
fetchRequest.resultType = .dictionaryResultType
fetchRequest.returnsObjectsAsFaults = false

let expression = NSExpressionDescription()
expression.expression = NSExpression(forFunction: "sum:", arguments:[NSExpression(forKeyPath: "estimatedValue")])
expression.name = "sumOfEstimatedValue";
expression.expressionResultType = .decimalAttributeType

fetchRequest.propertiesToFetch = [expression];

do {
let opportunityAggregates = try stack.context.fetch(fetchRequest)
print(opportunityAggregates)
} catch {
print("Failed to fetch aggregates")
}

上面代码的结果是:

[{
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>";
})]

感谢您提供的任何帮助。

最佳答案

我在原来问题中的代码现在可以工作了。我不太确定出了什么问题,但从我的设备中删除应用程序数据并用完全相同的数据填充它可以修复它。

关于ios - Swift 3 核心数据 NSExpression 始终返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46028437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com