Iphone 通过核心数据计算十进制值的总和工作不正常?
我第一次投稿到这一轮,所以如果我没有正确遵守所有规则,请容忍我 我正在为iPhone(OS 3.1)编写一个应用程序,并试图编写一些代码,让我可以添加小数。我有一个名为SimpleItem的核心数据实体,它带有amount属性。以下是我编写的测试用例:Iphone 通过核心数据计算十进制值的总和工作不正常?,iphone,core-data,nsdecimalnumber,Iphone,Core Data,Nsdecimalnumber,我第一次投稿到这一轮,所以如果我没有正确遵守所有规则,请容忍我 我正在为iPhone(OS 3.1)编写一个应用程序,并试图编写一些代码,让我可以添加小数。我有一个名为SimpleItem的核心数据实体,它带有amount属性。以下是我编写的测试用例: // Create and configure objects SimpleItem *si1 = [self createSimpleItem:@"si1"]; si1.amount = [NSDecimalNumber dec
// Create and configure objects
SimpleItem *si1 = [self createSimpleItem:@"si1"];
si1.amount = [NSDecimalNumber decimalNumberWithMantissa:1000 exponent:0 isNegative:NO];
SimpleItem *si2 = [self createSimpleItem:@"s12"];
si2.amount = [NSDecimalNumber decimalNumberWithMantissa:2000 exponent:0 isNegative:NO];
// Need to save prior to fetching results with NSDictionaryResultType (known limitation)
[self save];
// Describe fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"SimpleItem" inManagedObjectContext:self.context];
[request setEntity:entityDescription];
[request setResultType:NSDictionaryResultType];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"amount"];
// For whatever reason, evaluating this expression here is absolutely not working. Probably decimals aren't handled properly.
NSExpression *sumAmountExpression = [NSExpression
expressionForFunction:@"max:"
arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"amount"];
[expressionDescription setExpression:sumAmountExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
// Fetch the amounts
NSError *error = nil;
NSArray *array = [self.context executeFetchRequest:request error:&error];
如果我通过otest执行此代码并对其进行调试,则在执行提取请求时会出现异常:“-[NSDecimalNumber count]:发送到实例的无法识别的选择器。”
不过,只需在不使用聚合函数的情况下计算keyPathExpression就可以了
这个例子完全相同,所以我想知道我做错了什么。或者这只是一个bug
祝你一切顺利,
哈拉尔德我想我以前有过这个问题。IIRC,问题是您已将获取结果类型设置为
NSDictionaryResultType
,但您正在NSArray
中接收它。尝试将提取结果类型切换为NSManagedObjectResultType
,该类型会返回数组。文档中的所有示例都使用默认的NSManagedObjectResultType
在任何情况下,错误消息显然是由NSDecimal对象通过数组的count
消息发送的结果。您可以通过在id
中捕获结果来确认这一点。例如:
id *genericObj = [self.context executeFetchRequest:request error:&error];
NSLog("returned object=%@",genericObj);
NSLog("returned object class=%@",[genericObj class]);
谢谢你让我知道。问题是我实际上不想使用托管对象。毕竟,我只对数量感兴趣,这就是我从字典中得到的 另外,正如您所描述的,尝试调查这个问题并没有起作用,因为执行获取请求会立即失败,并且我没有得到任何回复 还有其他想法吗?是否有其他可能获得聚合值?有什么“最佳实践”吗?目前,我执行以下操作(继续执行上述代码): NSExpressionDescription*expressionDescription=[[NSExpressionDescription alloc]init]; [expressionDescription setName:@“金额”]; [expressionDescription setExpression:keyPathExpression]; [expressionDescription setExpressionResultType:NSDecimalAttributeType]; [请求setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]
// Fetch the amounts
NSError *error = nil;
NSArray *array = [self.context executeFetchRequest:request error:&error];
if (array == nil) {
STFail(@"Fetch request could not be completed: %@", [error localizedDescription]);
}
STAssertEquals([array count], (NSUInteger) 2, @"Number of fetched objects != 2");
// Manually calculate the sum
NSDecimalNumber *sum = nil;
for (NSDictionary * dictionary in array) {
NSDecimalNumber *amount = (NSDecimalNumber *) [dictionary valueForKey:@"amount"];
if (!sum) {
sum = amount;
} else {
sum = [sum decimalNumberByAdding:amount];
}
}
STAssertNotNil(sum, @"Sum is nil");
STAssertTrue([sum isEqualToNumber:[NSDecimalNumber decimalNumberWithMantissa:3000 exponent:0 isNegative:NO]],
@"Sum != 3000: %@", sum);
问候,,
Harald将给定的源代码复制到一个新的iPhone项目中,并在模拟器中运行,在这里效果很好。我是针对雪豹上的3.1.2 SDK进行编译的 这是我用于数据模型的内容:
你的问题一定是因为别的原因。您能描述一下您的模型或进一步简化它吗?当指定
NSDictionaryResultType
时,您将通过executeFetchRequest:error:
收到一个包含一个对象的数组。这是我从内存中提取出来的。嘿,Harald,请不要将您的回答作为问题的答案发布。相反,您应该使用“添加评论”功能直接回复答案/评论,并在找到问题时使用其他信息编辑问题。您需要一定的声誉才能发表评论,但更新原始问题也可以。。。只需添加一个更新信息的新部分,这样你仍然可以看到我遇到的原始问题;认为这是核心数据中的一个bug,因为它在SQL存储中工作,而不是在内存中。当我有机会的时候,我会把这件事归档在雷达上;我是说你在使用SQL存储?