我正在尝试获取UIPickerView
运行 App 时的选定值。但我得到错误,
致命错误:索引超出范围
我通过函数进行print("selected ==>\(selected)")
调试 。我通过调试numberOfRowsInComponent
得到的值为0。selected
如何解决这个问题呢?
Value From Postman
- Plandate function
http://xxx.xxx.xxx.xx/ProjectService/PODService.svc/GetPlanDate/2
"[ { \"PlanDate\": \"26/08/2019\", \"PlanDateFullFormat\": \"20190826\" }, { \"PlanDate\": \"27/08/2019\", \"PlanDateFullFormat\": \"20190827\" }]"
- PlanShipment
http://xxx.xxx.xxx.xx/ProjectService/PODService.svc/GetShipment/2/20190826
"[ { \"Shipment\": \"4505023278\" }, { \"Shipment\": \"4505023279\" }]"
http://xxx.xxx.xxx.xx/ProjectService/PODService.svc/GetShipment/2/20190827
"[ { \"Shipment\": \"4505023244\" }, { \"Shipment\": \"4505023274\" }]"
Return Value From JSON
- Plandate function
http://xxx.xxx.xxx.xx/ProjectService/PODService.svc/GetPlanDate/2
pplandateCategories ==>["20190826", "20190827"]
- PlanShipment
http://xxx.xxx.xxx.xx/ProjectService/PODService.svc/GetShipment/2/20190827
getpShipmentDCPlanData ==>["4505023244", "4505023274"]
Swift
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return pPlandateCategories.count
}else{
let selected = pickerview.selectedRow(inComponent:0)
return pShipmentCategories[selected].count ———>. Thread 1: Fatal error: Index out of range
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return pPlandateCategories[row]
}else{
let selected = pickerview.selectedRow(inComponent:0)
return pShipmentCategories[selected][row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0{
pickerview.reloadComponent(1)
}else{
let selected = pickerview.selectedRow(inComponent:0)
// getpPlanDatePickerView.text = "PlanDate:" + " " + pplandateCategories[selected][row]
someTextField.text = "Shipment:" + " " + pShipmentCategories[selected][row]
}
}
Error when Run Project
My WebService ==> http://xxx.xxx.xxx.xx/ProjectService/PODService.svc/GetPlanDate/2GetPlanDate/2
Fatal error: Index out of range
2019-12-19 10:55:14.459056+0700 pickerDateAndShipment[782:19020] Fatal error: Index out of range