0

我一直在尝试学习如何使用 NSExpressions 从核心数据中获取聚合信息。我的 fetch 工作正常,但我在尝试解析结果数据以进行处理时遇到了麻烦。我确定我一定错过了一些东西,但我无法弄清楚它是什么。我正在获取某些实体的属性总和,并且这部分是正确的。然后我想对结果做一些数学运算,但我没有得到想要的结果。

这是我的示例代码

[_mainContext save:nil];
NSMutableDictionary *accountBalances = [[NSMutableDictionary alloc] init];
NSFetchRequest* request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"AccountTransaction"
                             inManagedObjectContext:_mainContext];

// build an expression to calculate the sum
NSExpressionDescription* totalOfTransactionsDescription = [[NSExpressionDescription alloc] init];

NSExpression *keyExpression = [NSExpression expressionForKeyPath:@"amount"];
NSExpression *theExpression = [NSExpression expressionForFunction:@"sum:"
                                                        arguments:@[keyExpression]];

[totalOfTransactionsDescription setName:@"totalOfTransactions"];
[totalOfTransactionsDescription setExpression:theExpression];
[totalOfTransactionsDescription setExpressionResultType:NSFloatAttributeType];

// we want the results in a dictionary
request.resultType = NSDictionaryResultType;
request.propertiesToFetch = @[totalOfTransactionsDescription];
NSArray* results;
NSDictionary* fetchResultsDictionary;
NSDecimalNumber *total;

// credits
request.predicate = [NSPredicate predicateWithFormat:@"isCredit == %@",[NSNumber numberWithBool:YES]];
results = [_mainContext executeFetchRequest:request error:nil];
fetchResultsDictionary = [results objectAtIndex:0];
total = [fetchResultsDictionary objectForKey:@"totalOfTransactions"];
[accountBalances setObject:total forKey:@"postedCredits"];

// debits
request.predicate = [NSPredicate predicateWithFormat:@"isCredit == %@",[NSNumber numberWithBool:NO]];
results = [_mainContext executeFetchRequest:request error:nil];
fetchResultsDictionary = [results objectAtIndex:0];
total = [fetchResultsDictionary objectForKey:@"totalOfTransactions"];
[accountBalances setObject:total forKey:@"postedDebits"];


// build an account balance
NSDecimalNumber *startingBalance = [NSDecimalNumber decimalNumberWithString:@"100.00"];
NSDecimalNumber *currentBalance = startingBalance;

// add deposits
currentBalance = [currentBalance decimalNumberByAdding:[accountBalances objectForKey:@"postedCredits"]];

// subtract debits
currentBalance = [currentBalance decimalNumberBySubtracting:[accountBalances objectForKey:@"postedDebits"]];

[accountBalances setObject:currentBalance forKey:@"currentBalance"];

NSLog(@"%@", accountBalances);

这是我的控制台日志

2013-08-23 23:56:35.775 NSExpressionCoreDataTestApp[5162:907] {
    currentBalance = 100;
    postedCredits = 300;
    postedDebits = 25;
}

现在我不是火箭科学家,但 $100+$300-$25 = $375。我的 currentBalance 总是 100 美元。同样根据我的示例数据,postedCredits 和 postedDebits 是正确的,但似乎没有任何东西被添加到初始起始余额中或从初始余额中减去。我确定我必须在某处输入结果,但我已经尝试在获取结果添加到字典之前以及在加法或减法发生之前尝试过。我什至尝试过 [NSDecimalNumber NSDecimalNumberWithString:] 从字典中提取它们,然后再对它们进行操作。

如果有人能在这里指出我正确的方向,我将不胜感激。提前致谢!!

4

1 回答 1

0

今天早上用新的眼睛看了之后,我意识到答案正盯着我的脸。

[totalOfTransactionsDescription setExpressionResultType:NSFloatAttributeType];

本来应该

[totalOfTransactionsDescription setExpressionResultType:NSDecimalAttributeType];

这一下子就搞定了。:-)

于 2013-08-24T15:02:35.603 回答