iOS 应用程序因 NSDecimalNumber 的错误值而终止

iOS App terminates due to bad value of NSDecimalNumber

我的 iOS 应用程序在 Xcode 模拟器中终止,原因是下一行 EXC_BAD_ACCESS 错误

if ([amount.text isEqualToString:@""] || [chargeAmount floatValue] == 0.0) {

完整的代码块是

if ([amount.text isEqualToString:@""] || [chargeAmount floatValue] == 0.0) {
    // Focus on the poptip target.
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [transactionView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    [self performSelector:@selector(amountRequiredPopup) withObject:nil afterDelay:0.3];
    return;
}

我检查了 NSLog 中的 amount.text,它显示的是值而不是 ChargeAmount。 chargeAmount 的值似乎有误。 chargeAmount在.h文件的interface中定义如下:

NSDecimalNumber* chargeAmount;

我是 objective C 和 iOS 应用程序开发的新手。请指出问题出在哪里?提前谢谢你。

我建议您可以将该变量定义为 float 而不是 NSDecimalNumber* chargeAmount; 并将其值设置为 viewDidLoad() 中的 0.0 然后稍后您可以将其设置为根据您的代码并添加该检查。

根据您最后的评论,我认为问题在于您已将另一种类型的值分配给 chargeAmount 变量,搜索与 chargeAmount 和 [=20 相关的所有代码=] 在那里,在代码的某些部分中,您将 NSMutableAttributedString 类型的值分配给 chargeAmount 变量,然后当您调用 [chargeAmount floatValue] 时崩溃,因为 NSMutableAttributedString 没有调用任何方法floatValue

希望对您有所帮助