title: Calculating sum for decimal values via Core Data not working properly?
body: <p>first time I post to this round, so please bear with me if I don't follow all the rules properly.</p>
  
  <p>I am writing an app for the iPhone (OS 3.1) and am trying to write some code which lets me add decimals. I have a Core Data entity called SimpleItem with a amount attribute. Here is the test case I wrote:</p>
  
  <pre><code>// 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:&amp;error];
  </code></pre>
  
  <p>If I execute this code through otest and debug it, I get an exception when the fetch request is executed: "-[NSDecimalNumber count]: unrecognized selector sent to instance."</p>
  
  <p>Just evaluating the keyPathExpression without the aggregate function works fine, though.</p>
  
  <p>The <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484-SW1" rel="nofollow noreferrer">reference documentation</a> shows exactly the same example so I'm wondering what I am doing wrong. Or could this be just a bug?</p>
  
  <p>All the best,
  Harald</p>