积累NSDecimalNumber值的正确方法是什么?

时间:2014-11-19 17:05:03

标签: objective-c nsdecimalnumber

我正在尝试累积从数组中获取的NSDecimalNumber值( apptDataArray ,从CoreData存储中获取; aCurrentCharges定义为NSDecimalNumber)。这是我的代码:

        NSDecimalNumber *accumulatedFees = 0;
        for(int i = 0; i < apptDataArray.count; i++)  {
            AppointmentInfo *currentAppointment = [apptDataArray objectAtIndex: i];
            accumulatedFees = [accumulatedFees decimalNumberByAdding: currentAppointment.aCurrentCharges];  //  <----  not accumulating
        }
        oAccumulatedCharges.text = [NSString stringWithFormat:@"%@",accumulatedFees];

我的问题是标记不累积的行就是这样 - 不累积。我做错了什么?

1 个答案:

答案 0 :(得分:3)

变量accumulatedFees未正确初始化。尝试这样设置:

NSDecimalNumber *accumulatedFees = [NSDecimalNumber zero];