这里是绝对的新秀,在试错中跌跌撞撞。尽管在这里搜索了几个小时并尝试了许多不同的想法,但我遇到了一个无法解决的问题。我收到的错误是(TotalFuelLoad.text
行):
表达过于复杂,无法在合理的时间内解决;考虑将表达式分成不同的子表达式
代码如下:
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var LeftMainTankQuantity: UITextField!
@IBOutlet weak var RightMainTankQuantity: UITextField!
@IBOutlet weak var AuxTankQuantity: UITextField!
@IBOutlet weak var TailTankQuantity: UITextField!
@IBOutlet weak var TotalFuelLoad: UILabel!
@IBAction func TankFuelChanged(_ sender: Any) {
let LeftMainTankQuantityValue = Int(LeftMainTankQuantity.text!)
let RightMainTankQuantityValue = Int(RightMainTankQuantity.text!)
let AuxTankQuantityValue = Int(AuxTankQuantity.text!)
let TailTankQuantityValue = Int(TailTankQuantity.text!)
TotalFuelLoad.text = String(describing: (LeftMainTankQuantityValue ?? 0) + (RightMainTankQuantityValue ?? 0) + (AuxTankQuantityValue ?? 0) + (TailTankQuantityValue ?? 0))
}
}
任何想法或建议将不胜感激。
答案 0 :(得分:2)
我建议分三个阶段进行操作:
??
来安全地解开包装并将您的值转换为Int
。total
。total
转换为String
。@IBAction func TankFuelChanged(_ sender: Any) {
let leftMainTankQuantityValue = Int(LeftMainTankQuantity.text ?? "") ?? 0
let rightMainTankQuantityValue = Int(RightMainTankQuantity.text ?? "") ?? 0
let auxTankQuantityValue = Int(AuxTankQuantity.text ?? "") ?? 0
let tailTankQuantityValue = Int(TailTankQuantity.text ?? "") ?? 0
let total = leftMainTankQuantityValue + rightMainTankQuantityValue + auxTankQuantityValue + tailTankQuantityValue
TotalFuelLoad.text = "\(total)"
}