Does calculating the sum for decimal values ​​via Core Data not working properly?

The first time I send to this round, so please carry me if I do not follow all the rules correctly.

I am writing an application for the iPhone (OS 3.1) and am trying to write code that allows me to add decimal numbers. I have a Core Data object called SimpleItem with a quantity attribute. Here is an example I wrote:

// 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];

If I execute this code through otest and debug it, I get an exception when a fetch request is executed: "- [NSDecimalNumber count]: unrecognized selector sent to the instance."

Just evaluating keyPathExpression without an aggregate function works fine.

, , . ?

, Harald

+3
3

, . 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]);
0

, . , . , , , .

, , , , , .

? ? " "? ( ):

NSExpressionDescription * expressionDescription = [[NSExpressionDescription alloc] init];   [Description setName: @ "amount" ];   [Description setExpression: keyPathExpression];   [Description setExpressionResultType: NSDecimalAttributeType];   [request setPropertiesToFetch: [NSArray arrayWithObject: Description]];

// 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

0
0

Source: https://habr.com/ru/post/1722258/


All Articles