我知道不应该问这种问题。但是,我在这里呆了几天,毫无头绪。所以我真的需要帮助。
我有一个核心数据对象,比方说,产品。
//产品
NSDecimalNumber *数量
NSDecimalNumber *价格
我想做的是总结一下价格,然后把它贴在标签上。我在这里搜索并找到了一些主题: NSDecimalNumber不能执行标准的匹配操作,因为它是一个包装实际值的对象。这必须通过decimalNumberByAdding和decimalNumberByMultiplyingBy.来完成。所以,我写了以下代码,
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);
}];所有这些NSLog都没有显示正确的结果。它们没有显示0或NULL。
但是,如果我NSLog下面的代码,则可以显示正确的结果。
[product.price decimalNumberByMultiplyingBy:product.quantity]你能帮我指出我错过了什么吗?
发布于 2012-07-19 11:35:20
您没有分配返回值。
[totalPrice decimalNumberByAdding:[product.price decimalNumberByMultiplyingBy:product.quantity]];应:
totalPrice = [totalPrice decimalNumberByAdding:[product.price decimalNumberByMultiplyingBy:product.quantity]];因为decimalNumberByAdding返回一个值,所以不会自动更新变量。因此,totalPrice始终是0,这是您分配给init的值。
https://stackoverflow.com/questions/11559835
复制相似问题