如何解决问题 UIPickerView 致命错误:索引超出范围 [IOS Swift 5.1 Xcode 11.2]
Posted
技术标签:
【中文标题】如何解决问题 UIPickerView 致命错误:索引超出范围 [IOS Swift 5.1 Xcode 11.2]【英文标题】:How to Solve problem UIPickerView Fatal error: Index out of range [IOS Swift 5.1 Xcode 11.2] 【发布时间】:2019-12-19 09:05:23 【问题描述】:我正在尝试获取UIPickerView
的选定值
运行应用程序时。但我得到错误,
致命错误:索引超出范围
我通过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
【问题讨论】:
【参考方案1】:只有在异步加载组件的数据并且在第一次重新加载表视图时数据源为空时才会发生崩溃。
为了避免崩溃检查pShipmentCategories
数组是否为空
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if component == 0
return pPlandateCategories.count
else
let selected = pickerview.selectedRow(inComponent:0)
return pShipmentCategories.isEmpty ? 0 : pShipmentCategories[selected].count
【讨论】:
我收到更多错误致命错误:在标题箭头 didSelectRow 函数let selected = pickerview.selectedRow(inComponent:1) return pShipmentCategories.isEmpty ? 0 : pShipmentCategories[selected][row]
中调试后索引超出范围@ -> 如果使用 0 无法将“Int”类型的返回表达式转换为“字符串”类型?和致命错误:索引超出范围
didSelectRow
应该可以在没有任何更改的情况下工作。潘乔的建议是错误的
感谢您的回答,didSelectRow。我写对了吗? ` func pickerView (_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) if component == 0 pickerview.reloadComponent (1) else let selected = pickerview.selectedRow (inComponent: 1) print ("selected didSelectRow ==> \ (selected)") someTextField.text = "Shipment:" + "" + pShipmentCategories [selected] [row] -> 致命错误:索引超出范围 `
谢谢解答,我的App运行正常没有报错以上是关于如何解决问题 UIPickerView 致命错误:索引超出范围 [IOS Swift 5.1 Xcode 11.2]的主要内容,如果未能解决你的问题,请参考以下文章
致命错误:索引超出范围 / UIPickerView /Array/ Swift
Swift UIPickerView 最后一列数据导致致命崩溃
如果显示致命错误,如何解决激活 WooCommerce 的问题?
如何解决致命错误 LNK1000:IncrBuildImage 期间的内部错误?