I know this kind of question shouldn’t be asked. But, I stuck here for days without any clue. So, I really need help.
I have a core data object, let’s say, products.
// Product
NSDecimalNumber *quantity
NSDecimalNumber *price
What I’m trying to do is summarize the price and set it to a label. I search and found some topic here said that NSDecimalNumber can’t do standard match operation as it’s an object that wrap the actual value. It has to be done through decimalNumberByAdding and decimalNumberByMultiplyingBy. So, I wrote the following code,
NSDecimalNumber *totalPrice = [[NSDecimalNumber alloc] initWithDouble:0.0];
[self.productArray enumerateObjectsUsingBlock:^(Product *product, NSUInteger idx, BOOL *stop) {
[totalPrice decimalNumberByAdding:[product.price decimalNumberByMultiplyingBy:product.quantity]];
NSLog(@"%@", totalPrice);
NSLog(@"%@", totalPrice.doubleValue);
NSLog(@"%@", totalPrice.decimalValue);
}];
None of those NSLog were showing correct result. They were showing neither 0 or NULL
But, if I NSLog the following code, the correct result can be shown.
[product.price decimalNumberByMultiplyingBy:product.quantity]
Can you help me point out what do I miss here?
You are not assigning the return value.
should be:
Since decimalNumberByAdding returns a value, does not update the variable automatically. Because of that, totalPrice is always 0, that’s the value you assigned on init.