Swift 5 PickerView Array Count 工作不正常,无法使用身高和体重计算用户 BMI

Swift 5 PickerView Array Count is not working properly enough to calucate user BMI using Height and Weight

使用 2 个带数组的 PickerView 控制器来尝试计算用户的 BMI,但重量选择器计数给我一个线程 1:致命错误:索引超出范围。

我能做些什么来解决这个问题吗?[iPhone StoryBoard 图片]

        //Height, Country and Weight Labels and Pickers
        @IBOutlet weak var bhLabel: UILabel!
        @IBOutlet weak var weightCalLabel: UILabel!
        @IBOutlet weak var heightbmiLabel: UILabel!
        @IBOutlet weak var heightLabel: UILabel!
        @IBOutlet weak var heightScrollMenu: UIPickerView!
        @IBOutlet weak var weightLabel: UILabel!
        @IBOutlet weak var weightScrollMenu: UIPickerView!
        let heightToInches = (12...90).map { String([=11=]) }
        let weightOptions  =  (50...600).map  { String([=11=]) }

        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }

        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            if pickerView.tag == 31 {
                return weightOptions.count

            } else {
                return heightToInches.count
                }
            }

        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            if pickerView.tag == 30 {
                return "\(heightToInches[row])" + " Inches"
            }

            else {
                return "\(weightOptions[row])" + " Lbs"
            }
        }

        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if pickerView.tag == 30 {
                heightLabel.text = String(heightToInches[row]) + " Inches"
                heightbmiLabel.text = "(Height " + String(heightToInches[row]) + " Inches)²"
            }
            if pickerView.tag == 31 {
                weightLabel.text =  String(weightOptions[row]) + " lbs"
                weightCalLabel.text = "Weight " + String(weightOptions[row]) + " lbs"
            }

Blockquote If I delete everything below then my app runs fine without any issues.

让 stringHeight = self.heightToInches 让 IntHeight = Int(stringHeight[row])

            print(stringHeight[row])

            let stringWeight = self.weightOptions

Blockquote let IntWeight = Int(stringWeight[row]) This is where i get an error but whenever I delete this line of code then my app does not experience the Fatal Error: Index out of Range Error

   If I delete everything below this line then I can use both pickerviews without any issues and select any 

            print("IntWeight = \(IntWeight!).")

            let bmiconstant = 703

            let CalWeight = Int(IntWeight!)
            print("Cal Weight = \(CalWeight)")

            let CalHeight = Int(IntHeight!)
            print("CalHeight = \(CalHeight) ")

            let heightSquared = CalHeight * CalHeight
            print("HeightSquared = \(heightSquared)")

            let WeightDividedByHeight: Double = Double(CalWeight) / Double(heightSquared)

            let userBMI: Double = Double(bmiconstant) * Double(WeightDividedByHeight)

            print("user bmi = \(userBMI)")
            print("-------------------------")
            print("")
            print("")
            print("")
        }

您在 didSelectRow 中的一次转化似乎调用了超出数据范围或数据范围的索引,例如:

let stringHeight = self.heightToInches 
let IntHeight = Int(stringHeight[row])

确保它安全地包含在 if 语句中或使用 guard 确保它存在。