Swift:在国家和城市之间的两个pickerView中解析Json

Posted

技术标签:

【中文标题】Swift:在国家和城市之间的两个pickerView中解析Json【英文标题】:Swift: Json parse in two pickerView between country and city 【发布时间】:2017-10-09 05:04:07 【问题描述】:

我有两个 JSON 网址。想要在两个 UIPickerView 之间进行解析。一个 UIPickerView 代表国家,另一个 UIPickerView 代表城市。我已经能够在 Country 的第一个 UIPickerView 中解析 JSON,这里的数据非常完美。但是当单击城市的 UIPickerView 时,我在 DidSelectRow 函数中收到了一条线程消息。请尝试解决这个问题

这是我的代码。

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

关于down代码的线程是“线程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

    


【问题讨论】:

确保您的网点都已连接。可能是您的cityTextField 插座未连接。 如果让也试试。可能值为零。试试***.com/questions/36871398/… 兄弟你在哪一行遇到这个问题 @Anbu.Karthik self.cityTextField.text = self.arrCitiPicker[row].name 在 didselectRow 函数行中 你在当前班级中连接了cityTextField 【参考方案1】:

每当您更改国家/地区时,您都需要更新城市选择器数组。

在国家选择器的选择中,为特定国家/地区 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)

更新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()

【讨论】:

感谢您的回答。我必须为更新 arrCityPicker 写什么 为城市调用网络服务并将响应数据添加到数组中。但首先打印你的城市数组。您的错误可能是 nil 值。 在使用您的新代码后,我在此行 self.cityTextField.text = self.arrCitiPicker[row].name 上得到了线索。控制台消息也是Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo=NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set. fatal error: Index out of range 只需在 updateCitiesFor() 函数中更新数组后打印您的 arrCitiPicker。调试区显示什么? 它什么也不打印print( "city picker \(self.cityPickerDataLoad(id: row))")

以上是关于Swift:在国家和城市之间的两个pickerView中解析Json的主要内容,如果未能解决你的问题,请参考以下文章

无法从 Swift 中的 Facebook iOS SDK 捕获城市和国家/地区值

[BZOJ3456]城市规划

JZOJ3303城市规划

2013集训胡渊鸣城市规划

[bzoj3456]城市规划:多项式,分治

边双联通分量