I have a 2 texview and 2 pickerview.I want user to select the city and district of that city. When I select the city I want to list the districts of that city in the pickerview. I don't use city model. When selected city When the city is selected, I want the districts of that city to appear in pickerview. How to do this ?
my JSON Data
{"data": [ {"il_adi": "Adana", -- cityNames"plaka_kodu": " 01","alan_kodu": "0322","nufus": "2.274.106","bolge": "Akdeniz","yuzolcumu": "13.844","nufus_artisi": "4,7%","erkek_nufus_yuzde": "49.15%","erkek_nufus": "1.106.811","kadin_nufus_yuzde": "50.15%","kadin_nufus": "1.113.314","nufus_yuzdesi_genel": "2,71%","ilceler": [ -- districts {"plaka_kodu": "1","ilce_kodu": "1757","il_adi": "ADANA","ilce_adi": "ALADAĞ", --- district name"nufus": "15.855","erkek_nufus": "8.194","kadin_nufus": "7.661" }
my data function
func citylİST(){ if let path = Bundle.main.path(forResource: "city", ofType: "json") { let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe) let jsonResult = try? (JSONSerialization.jsonObject(with: data!, options: .fragmentsAllowed) as! NSDictionary) if let cityList = jsonResult?.value(forKey: "data") as? NSArray{ for city in cityList{ if let cityDict = city as? NSDictionary{ if let cityName = cityDict.value(forKey: "il_adi") as? String{ self.cityArray.append(cityName) } if let ilceName = cityDict.value(forKey: "ilceler") as? NSArray{ for ilce in ilceName{ if let ilceDict = ilce as? NSDictionary{ if let ilceName = ilceDict.value(forKey: "ilce_adi") as? String{ self.ilceArray.append(ilceName) print(ilceName) } } } } } } } } }
my pickerview codes
extension searchView : UIPickerViewDelegate, UIPickerViewDataSource{ func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { switch pickerView.tag { case 1: return cityArray.count case 2: return ilceArray.count default: return 1 } } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { switch pickerView.tag { case 1: return cityArray[row] case 2: return ilceArray[row] default: return "Data Not Found" } } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { switch pickerView.tag { case 1: searchText.text = cityArray[row] selectedCity = cityArray[row] case 2: ilceText.text = ilceArray[row] selectedİlce = ilceArray[row] default:break