2017-10-09 111 views
0

我有兩個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 

    } 

} 
+0

確保您的網點都連接。可能是你的'cityTextField'插座沒有連接。 – rmaddy

+0

如果讓也試試。可能價值是零。嘗試https://stackoverflow.com/questions/36871398/thread-1-exc-bad-instruction-code-exc-1386-invop-subcode-0x0 –

+0

其中你遇到了這個問題bro –

回答

0

無論何時更改國家/地區,您都需要更新城市選取器陣列。

在做了選擇國家選擇器,呼叫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() 
} 
+0

謝謝你的回答。我必須寫更新arrCityPicker – Minzu

+0

調用城市的Web服務並將響應數據添加到數組中。但首先打印你的城市陣列。你的錯誤可能沒有價值。 –

+0

現在我有這個「致命錯誤:索引超出範圍」 – Minzu