Swift 国家选择器指数超出范围

Swift 国家选择器指数超出范围,swift,Swift,我为我的约会选择人准备了这个代码, 但我在这一行中得到一个错误: return pickerData[selectedCountry].state.count 致命错误:索引超出范围 我不知道, 我的代码有什么问题 选择器的数据: var stateData = CountryData(country: "USA", state: [StateData(state: "New York", city: ["New York", "Buffalo", "LockPort"])]) 请提供更多

我为我的约会选择人准备了这个代码, 但我在这一行中得到一个错误:

return pickerData[selectedCountry].state.count
致命错误:索引超出范围

我不知道, 我的代码有什么问题

选择器的数据:

var stateData = CountryData(country: "USA", state: [StateData(state: "New York", city: ["New York", "Buffalo", "LockPort"])]) 

请提供更多信息,“selectedCountry”在崩溃时的值是多少。您确定之前已正确初始化它吗?首先,我建议您在返回的行上设置断点,以了解selectedCountry index的实际值。此外,在选择组件中的行的方法中不使用
row
。因此,在
pickerView.selectRow(0,不完整:1,动画:true)
中,您应该使用
行而不是0。最好使用
组件
而不是它的值(1)!选择器的数据必须在override func viewDidLoad()下。
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

    {

     switch component {

        case 0: // country

            selectedCountry = pickerView.selectedRow(inComponent: 0)

            selectedState = 0

            selectedCity = 0



            pickerView.reloadComponent(1)

            pickerView.selectRow(0, inComponent: 1, animated: true)

            pickerView.reloadComponent(2)

            pickerView.selectRow(0, inComponent: 2, animated: true)


        case 1: // country

            selectedState = pickerView.selectedRow(inComponent: 1)

            selectedCity = 0

            pickerView.reloadComponent(2)

            pickerView.selectRow(0, inComponent: 2, animated: true)

        case 2: // country

            selectedCity = pickerView.selectedRow(inComponent: 2)


        default:

            break

        }


        print("\(pickerData[selectedCountry].country), \(pickerData[selectedCountry].state[selectedState].state), \(pickerData[selectedCountry].state[selectedState].city[selectedCity])")

    }

      func numberOfComponents(in pickerView: UIPickerView) -> Int

      {

                return 3

      }

       func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

       {

        switch  component {

        case 0:

            return pickerData.count

        case 1:

                return pickerData[selectedCountry].state.count

        case 2:

            return pickerData[selectedCountry].state[selectedState].city.count

        default:

            return 0

        }
       }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?

    {

        switch  component {

        case 0:

            return pickerData[row].country

        case 1:

            return pickerData[selectedCountry].state[row].state

        case 2:

            return pickerData[selectedCountry].state[selectedState].city[row]

        default:

            return ""

        }