objective c - Problems with NSDecimalNumber Arithmetic when NSDecimalNumbers are stored in NSDictionary from NSExpression Core Data Fetch -


i have been experimenting trying learn how use nsexpressions fetch aggregate info core data. have fetch working i'm having trouble trying parse resulting data processing. i'm sure must missing can't figure out is. i'm fetching sum of attribute on entities , have part right. want math on results not getting desired results.

here sample code

[_maincontext save:nil]; nsmutabledictionary *accountbalances = [[nsmutabledictionary alloc] init]; nsfetchrequest* request = [[nsfetchrequest alloc] init]; request.entity = [nsentitydescription entityforname:@"accounttransaction"                              inmanagedobjectcontext:_maincontext];  // build expression calculate 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];  // want results in 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 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); 

here console log

2013-08-23 23:56:35.775 nsexpressioncoredatatestapp[5162:907] {     currentbalance = 100;     postedcredits = 300;     posteddebits = 25; } 

now i'm no rocket scientist, $100+$300-$25 = $375. currentbalance $100. based on sample data postedcredits & posteddebits correct nothing seems getting added or subtracted initial starting balance. i'm sure must need type cast results somewhere i've tried before fetch result gets added dictionary & before addition or subtraction takes place. i've tried [nsdecimalnumber nsdecimalnumberwithstring:] after pulling them dictionary before operating on them.

if point me in right direction here i'd appreciate it. in advance!!

after looking fresh eyes morning realized answer staring me in face.

[totaloftransactionsdescription setexpressionresulttype:nsfloatattributetype]; 

should have been

[totaloftransactionsdescription setexpressionresulttype:nsdecimalattributetype]; 

that cleared right up. :-)


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -