Swift:Json解析两个pickerView在国家和城市之间
问题描述:
我有两个JSON网址。想要在两个UIPickerView之间解析。一个用于国家的UIPickerView,另一个用于城市。我已经能够在国家的第一个UIPickerView中解析JSON,在这里数据即将完美。但是当点击城市的UIPickerView时,我在DidSelectRow func中收到了一条线程消息。请尝试解决此问题Swift:Json解析两个pickerView在国家和城市之间
这里是我的代码。
var arrCountryPicker = [CountryPicker]()
var arrCitiPicker = [CityPicker]()
var id = 0
var country_id = 0
我的两个网址
let url = URL(string: "http://......./mobile-app/countries")
let url = URL(string: "http://......../mobile-app/city-by-country-id?country_id="+String(country_id))
PickerViewCode
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var country: Int = arrCountryPicker.count
if pickerView == cityPickerView {
country = self.arrCitiPicker.count
}
return country
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == countryPickerView {
let titleRow = arrCountryPicker[row].name
return titleRow
} else if pickerView == cityPickerView{
let titleRow = arrCitiPicker[row].name
return titleRow
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
countryTextField.text = arrCountryPicker[row].name
if pickerView == countryPickerView {
self.countryTextField.text = self.arrCountryPicker[row].name
self.country_id = self.arrCountryPicker[row].id!
self.countryPickerView.isHidden = true
}
else if pickerView == cityPickerView{
Thread about down code is "Thread 1: exc_bad_instruction(code=exc_1386_invop,subcode=0x0)
self.cityTextField.text = self.arrCitiPicker[row].name
self.cityPickerView.isHidden = true
}
self.view.endEditing(true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if (textField == countryTextField){
self.countryPickerView.isHidden = false
}
else if (textField == cityTextField){
self.cityPickerView.isHidden = false
}
}
答
无论何时更改国家/地区,您都需要更新城市选取器阵列。
在做了选择国家选择器,呼叫web服务特定国家id和更新城市阵列。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//Remove this line
//txtCountry.text = self.arrCountries[row].name
if pickerView == CountryPicker {
self.txtCountry.text = self.arrCountries[row].name
self.country_id = self.arrCountryPicker[row].id!
//Update city array according selected country
self.updateCitiesFor(id: row)
self.CountryPicker.isHidden = true
} else if pickerView == cityPicker {
self.txtCity.text = self.arrCities[row].name
self.cityPicker.isHidden = true
}
self.view.endEditing(true)
}
FUNC键更新arrCitiPicker
func updateCitiesFor(id: Int) {
let url = URL(string: "http://......../mobile-app/\(id)")
//Call web service and store cities data of selected country into arrCitiPicker
//Clear city textfield when country change
self.txtCity.text = ""
//Update City Picker with new data
cityPicker.reloadAllComponents()
}
确保您的网点都连接。可能是你的'cityTextField'插座没有连接。 – rmaddy
如果让也试试。可能价值是零。尝试https://*.com/questions/36871398/thread-1-exc-bad-instruction-code-exc-1386-invop-subcode-0x0 –
其中你遇到了这个问题bro –