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

发布于 2025-01-08 21:24:38 字数 5360 浏览 1 评论 0原文

这是“有点”紧急,因为我的应用程序今天刚刚上线。

我的应用程序在模拟器中运行良好,当我将其复制到手机、iPad 和其他 iPhone 时也运行良好。我们对应用程序进行了测试。我将其提交到应用商店,现在已获得批准并上线,但仅在下载的应用商店应用程序中才会出现错误。

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

有人见过这样的事情吗?

我知道这篇文章完全含糊不清,但试图找出如果模拟器中的代码工作正常的话从哪里开始调试。

--更新--添加代码

 -(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];
}

}

This is 'sorta' urgent since my app just went live today.

My app works fine in the emulator, fine when I copy it to my phone, iPad and other iPhones. We tested the heck out of the app. I submitted it to the appstore and it is now approved and live but an error occurs only with the downloaded appstore app.

Basically i am rebalancing a bill using NSDecimal and number formatters. In the emulator and phone when I step through the code all is well. But the appstore bundle wigs out. It almost appears that the data that I am adding and subtracting is not being initalized.

Has anyone ever seen anything like this?

I know this post is completely vague but trying to figure out where to start debugging if the code in emulator works fine.

--UPDATE-- adding code

 -(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];
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花辞树 2025-01-15 21:24:38

我认为问题是您尝试使用 UITableViewCells 进行数据存储。您不将数据推送到单元格中,而是等待 UITableView 调用您的 cellForRowAtIndexPath 方法。

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

I think the problem is your attempt to use the UITableViewCells for data storage. You don't push data into the cells, you wait for the UITableView to call YOUR cellForRowAtIndexPath method.

The UITableView will only retain enough cells to fill the screen. As soon as one scrolls out of sight, it will be released. When your loop tries to retrieve it again with cellForRowAtIndexPath: it will allocate a new cell to give you. While your loop may initialize it, it won't be stored or used by the UITable object.

聽兲甴掵 2025-01-15 21:24:38

我的方法中存在两个变量的死存储问题。尽管我后来设置了它们,但我删除了它们并重新提交了应用程序。问题就这样消失了。

感谢您的提示。

There was a dead store issue with 2 variables in my method. Even though I set them later on I removed them and re-submitted the app. The issue went away.

Thanks for your tips.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文