我在学校做项目时正在学习Swift。我遇到了一个问题:
"线程1:致命错误:在展开可选值时出乎意料地发现nil"
在前面:
ResultLabel.text = "Вы должны потреблять \(Int(bmr)) килокалорий для поддержания веса.\nИндекс массы тела \(Int(bmi))."
这是我的代码:
class ViewController: UIViewController {
@IBOutlet weak var Text1: UITextField!
@IBOutlet weak var Text2: UITextField!
@IBOutlet weak var Text3: UITextField!
@IBOutlet weak var aktiv: UISegmentedControl!
@IBOutlet weak var sextype: UISegmentedControl!
@IBOutlet weak var traning: UISegmentedControl!
@IBOutlet weak var ResultLabel: UILabel!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
@IBAction func result(_ sender: Any) {
var bmr: Double = 0
var bmi: Double = 0
if let age: Int = Int(Text1.text!) {
if let weight: Int = Int(Text2.text!) {
if let height: Int = Int(Text3.text!) {
switch sextype.selectedSegmentIndex {
case 0:
bmr = 88.362 + 13.397 * Double(weight) + 4.799 * Double(height) - 5.677 * Double(age)
case 1:bmr = 447.593 + 9.247 * Double(weight) + 3.098 * Double(height) - 4.330 * Double(age)
default:
bmr = 0
}
bmi = Double(weight) / pow(Double(height) / 100, 2)
}
}
}
let factor = [1.375, 1.55, 1.725, 1.9, 2.7, 2.8, 3.6]
let selectedFactor = factor[traning.selectedSegmentIndex]
bmr *= selectedFactor
// Error on next line
ResultLabel.text = "Вы должны потреблять \(Int(bmr)) килокалорий для поддержания веса.\nИндекс массы тела \(Int(bmi))."
UIApplication.shared.keyWindow!.endEditing(true)
}
单击按钮后,我必须在ResultLabel中看到下一页和有关BMI和BMR的信息。