debugging 在Swift中展开一个可选项时发现nil,即使它正在被检查

mspsb9vt  于 2023-03-03  发布在  Swift
关注(0)|答案(1)|浏览(136)

非常基本的代码

我在学校写代码,必须创建一个应用程序。应用程序的一部分是用水量计算器。即使我检查可选的nil值,它也会得到错误:
“致命错误:展开可选”“时意外找到nil
它在func btnCalculate的随机部分发现此错误。

import UIKit

class WaterCalcViewController: UIViewController {
    
    //Inputs
    @IBOutlet weak var txtShowers: UITextField!
    @IBOutlet weak var txtBaths: UITextField!
    @IBOutlet weak var txtDishwasher: UITextField!
    @IBOutlet weak var txtWashingMachine: UITextField!
    @IBOutlet weak var txtTeeth: UITextField!
    @IBOutlet weak var txtToilet: UITextField!
    @IBOutlet weak var txtNumOfPeople: UITextField!
    //Inputs for the user to add values to their respective use of each item/thing
    
    //Outputs
    
    @IBOutlet weak var lblShowerError: UILabel!
    @IBOutlet weak var lblBathError: UILabel!
    @IBOutlet weak var lblDishwasherError: UILabel!
    @IBOutlet weak var lblWashingMachineError: UILabel!
    @IBOutlet weak var lblTeethError: UILabel!
    @IBOutlet weak var lblToiletError: UILabel!
    @IBOutlet weak var lblNumOfPeopleError: UILabel!
    @IBOutlet weak var lblTotalSum: UILabel!
    //Labels that appear when no value is given for their respective text field
    
    //Functions
    
    //Actions
    @IBAction func btnBack(_ sender: Any) {
        let destinationVC = self.storyboard?.instantiateViewController(identifier: "tabController") as! UITabBarController
        destinationVC.selectedIndex = 3
        destinationVC.modalPresentationStyle = .fullScreen
        self.present(destinationVC, animated: true, completion: nil)
    }
    
    var success: Int = 0
    
    @IBAction func btnCalculate(_ sender: Any) {
        success = 0
        let shower: Int? = Int(txtShowers.text!)
        if shower != nil {
            success = success + 1
        } else if shower == nil {
            lblShowerError.text = ("Please input a valid number")
        }
        
        let bath: Int? = Int(txtShowers.text!)
        if bath != nil {
            success = success + 1
        } else if bath == nil {
            lblBathError.text = ("Please input a valid number")
        }
        
        let dishwasher: Int? = Int(txtShowers.text!)
        if dishwasher != nil {
            success = success + 1
        } else if dishwasher == nil {
            lblDishwasherError.text = ("Please input a valid number")
        }
        
        let washingMachine: Int? = Int(txtShowers.text!)
        if washingMachine != nil {
            success = success + 1
        } else if washingMachine == nil {
            lblWashingMachineError.text = ("Please input a valid number")
        }
        
        let teethLength: Int? = Int(txtShowers.text!)
        if teethLength != nil {
            success = success + 1
        } else if teethLength == nil {
            lblTeethError.text = ("Please input a valid number")
        }
        
        let toiletFlushes: Int? = Int(txtShowers.text!)
        if toiletFlushes != nil {
            success = success + 1
        } else if toiletFlushes == nil {
            lblToiletError.text = ("Please input a valid number")
        }
        
        let numOfPeople: Int? = Int(txtShowers.text!)
        if numOfPeople != nil {
            success = success + 1
        } else if numOfPeople == nil {
            lblNumOfPeopleError.text = ("Please input a valid number")
        }
        
        if success == 7 {
            var totalSum = (shower! * 10)
            totalSum = totalSum + (bath! * 150)
            totalSum = totalSum + (dishwasher! * 30)
            totalSum = totalSum + (washingMachine! * 90)
            totalSum = totalSum + (teethLength! * 5)
            totalSum = totalSum + (numOfPeople! * 10)
            totalSum = totalSum + (toiletFlushes! * Int(4.5))
            lblTotalSum.text = ("Your average daily water use is \(totalSum)")
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Looks for single or multiple taps.
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
        
        view.addGestureRecognizer(tap)
    }
    
    //Calls this function when the tap is recognized.
    @objc func dismissKeyboard() {
        //Causes the view (or one of its embedded text fields) to resign the first responder status.
        view.endEditing(true)
    }
}
lc8prwob

lc8prwob1#

问题可能是您试图将一个nil值转换为Int。
在每一个这样的行中:-let shower: Int? = Int(txtShowers.text!)这里txtShowers.text可以是nil或者其他类似的值,尝试在这里设置一个断点。
您可以尝试这样做,而不是首先获得所有的值,你想要使用guardlet像这样:-
guard let shower = txtShowers.text else {\\Show some error or what you want}
这也会减少代码的数量或行数。
然后您就可以轻松地在shower上执行操作。
仅供参考:-您永远不应该在代码中使用强制转换,始终尝试使用guard letif let

相关问题