需要帮助非常奇怪的iPhone货币加法和减法

时间:2012-02-24 23:37:53

标签: iphone objective-c app-store nsdecimalnumber

由于我的应用程序今天刚刚上线,因此这种情况非常紧急。

我的应用程序在模拟器中工作正常,当我将其复制到手机,iPad和其他iPhone时很好。我们测试了应用程序。我已将其提交到appstore,现在已获批准并且已启用,但只有下载的appstore应用程序才会出现错误。

基本上我正在使用NSDecimal和数字格式化器重新平衡账单。在模拟器和手机中,当我单步执行代码时,一切都很顺利。但是appstore捆绑包就出来了。几乎看起来我正在添加和减去的数据没有被初始化。

有没有人见过这样的东西?

我知道这篇文章是完全模糊的,但是如果模拟器中的代码工作正常,那么试图弄清楚从哪里开始调试。

- 更新 - 添加代码

 -(void)balanceBill:(UITextField*)textField 
 {
//save the text field data
int row                         = [textField tag] - 900;
NSString *enteredSplitAmount    = [formatter stringWithNoCurrency:[textField text]];
[theGuestTotals replaceObjectAtIndex:row withObject:enteredSplitAmount];

//Get data object
BillDataObject* data    = [self theAppDataObject];

int changedSplitBy  = 0;
UITableViewCell *cell;
UITextField     *cellTextField;

double          adjustedBill = 0.0;
NSString        *guestAmountFromArray;
//NSString        *guestAmountAdjusted;

//Figure out how many guests did NOT have their bill changed & get new bill total
for (NSUInteger i=0; i < [theGuestTotals count]; i++)
{
    guestAmountFromArray        = [theGuestTotals objectAtIndex:i];
    if ([guestAmountFromArray   isEqualToString:data.splitByAmountChanged]) 
    {
        changedSplitBy++;
    }    
    //Adding ALL guest amounts to get a NEW Bill Total
    adjustedBill += [guestAmountFromArray doubleValue];
}

if (changedSplitBy == 0)
    changedSplitBy = 1;


//Convert newBill to decimal
NSDecimalNumber *adjustedBillTotal = [[NSDecimalNumber alloc] initWithDouble:adjustedBill];
NSDecimalNumber *originalBillTotal = [[NSDecimalNumber alloc] initWithString:data.totalBill];    

NSDecimalNumber *splitBy = [[NSDecimalNumber alloc] initWithInt:changedSplitBy];
NSDecimalNumber *updatedGuestAmount;

//Figure out the the difference is between the new amount and the old amount
NSDecimalNumber *adjustedBillTotalDifference = [originalBillTotal decimalNumberBySubtracting:adjustedBillTotal];

//figure out the difference each guest who did not have their bill changed difference
NSDecimalNumber *guestSplitAmountDifference = [adjustedBillTotalDifference decimalNumberByDividingBy:splitBy];

//loop through array of guest totals to see if a guest total if different from the original split amout
for (NSUInteger i=0; i < [theGuestTotals count]; i++)
{
    guestAmountFromArray = [theGuestTotals objectAtIndex:i];

    if ([guestAmountFromArray isEqualToString:data.splitByAmountChanged]) 
    {
        NSDecimalNumber *guestAmount = [[NSDecimalNumber alloc] initWithString:[theGuestTotals objectAtIndex:i]];

        NSIndexPath *indexPath  = [NSIndexPath indexPathForRow:i inSection:0];
        cell                    = [tableView cellForRowAtIndexPath:indexPath];
        cellTextField           = (UITextField*)[cell viewWithTag:i+900];

        //add the split amount to the guest amount
        updatedGuestAmount = [guestAmount decimalNumberByAdding:guestSplitAmountDifference];
        //update the textfield with UPDATED amount
        cellTextField.text      = [formatter stringWithNumberStyle:NSNumberFormatterCurrencyStyle numberToFormat:updatedGuestAmount];            
        //replace the guest amount in the array with the UPDATED amount
        [theGuestTotals         replaceObjectAtIndex:i withObject:[formatter stringWithNumberStyle:NSNumberFormatterDecimalStyle numberToFormat:updatedGuestAmount]];
    } else {
        //Not equal so just update the amount from what it was...
        //this might not be needed but I need to format if it is...
        cellTextField.text      = [formatter stringWithNumberStyle:NSNumberFormatterCurrencyStyle numberToFormat:            [NSDecimalNumber decimalNumberWithString:guestAmountFromArray]];
    }
}

//Now all guests who were not edited GOT updated now save that update for the next time this function is run
data.splitByAmountChanged = [formatter stringWithNumberStyle:NSNumberFormatterDecimalStyle numberToFormat:updatedGuestAmount];

//Clear out adjustedBill to get NEW totals after we updated each guest in the loop above
adjustedBill = 0;
//Lets see if we are over or under and do the 'REDISTRIBUTE'
for (NSUInteger i=0; i < [theGuestTotals count]; i++)
{
    adjustedBill += [[theGuestTotals objectAtIndex:i] doubleValue];
}

adjustedBillTotal = [[NSDecimalNumber alloc] initWithDouble:adjustedBill];

if ([originalBillTotal compare:adjustedBillTotal] == NSOrderedAscending) 
{  
    [warningImage setHidden:NO];
    NSDecimalNumber *overage = [adjustedBillTotal decimalNumberBySubtracting:originalBillTotal];

    [overUnderLabel setText:[NSString stringWithFormat:@"%@ over",[formatter stringWithNumberStyle:NSNumberFormatterCurrencyStyle numberToFormat:overage]]];

   // [self disableDone];
    [self enableRedistribute];
} 
else if ([originalBillTotal compare:adjustedBillTotal] == NSOrderedDescending) 
{
    [warningImage setHidden:NO];        
    NSDecimalNumber *underage = [originalBillTotal decimalNumberBySubtracting:adjustedBillTotal]; 

    [overUnderLabel setText:[NSString stringWithFormat:@"%@ under",[formatter stringWithNumberStyle:NSNumberFormatterCurrencyStyle numberToFormat:underage]]];

   // [self disableDone];
    [self enableRedistribute];
} 
else 
{
    [warningImage setHidden:YES];        
    overUnderLabel.text = @"";
    //[self enableDone];
    [self disableRedistribute];
}

}

2 个答案:

答案 0 :(得分:0)

我认为问题是您尝试使用UITableViewCells进行数据存储。你没有将数据推入单元格,等待UITableView调用你的cellForRowAtIndexPath方法。

UITableView只会保留足够的单元格来填充屏幕。只要一个人滚出视线,它就会被释放。当你的循环尝试使用cellForRowAtIndexPath再次检索它时:它将分配一个新的单元格给你。虽然你的循环可以初始化它,但它不会被UITable对象存储或使用。

答案 1 :(得分:0)

我的方法中存在2个变量的死存储问题。即使我稍后设置它们,我删除它们并重新提交应用程序。问题消失了。

感谢您的提示。