ios – How to show selected JSON data in the pickerview?


I have a 2 textviews and 2 pickerviews. I want the 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 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 ilce(){
    guard let path = Bundle.main.path(forResource: "yusif", ofType: "json") else {return}
   
    let url = URL(fileURLWithPath: path)
    do{
        let jsonData = try Data(contentsOf: url)
        
        selectedCity = try JSONDecoder().decode(cityOfModel.self, from: jsonData)
        
        if let selectedCity = selectedCity{
            print(selectedCity)
        }
    } catch{
        print("hataaaaa")
    }
}

My pickerview code:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    switch pickerView.tag {
    case 1:
        return selectedCity!.data.count
        
        
    case 2:
        return ???? 
        
    default :
        return 0
    }
   

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    switch pickerView.tag {
        case 1:
            return selectedCity!.data[row].ilAdi
        case 2:
            return selectedCity!.data[row].ilceler[row].ilceAdi
        default:
            return "HATA"
    }
}

My model:

struct cityOfModel: Codable {
    let data: [Datum]
}

// MARK: - Datum
struct Datum: Codable {
    let ilAdi, plakaKodu, alanKodu, nufus: String
    let bolge: Bolge
    let yuzolcumu, nufusArtisi, erkekNufusYuzde, erkekNufus: String
    let kadinNufusYuzde, kadinNufus, nufusYuzdesiGenel: String
    let ilceler: [Ilceler]
    let kisaBilgi: String

    enum CodingKeys: String, CodingKey {
        case ilAdi = "il_adi"
        case plakaKodu = "plaka_kodu"
        case alanKodu = "alan_kodu"
        case nufus, bolge, yuzolcumu
        case nufusArtisi = "nufus_artisi"
        case erkekNufusYuzde = "erkek_nufus_yuzde"
        case erkekNufus = "erkek_nufus"
        case kadinNufusYuzde = "kadin_nufus_yuzde"
        case kadinNufus = "kadin_nufus"
        case nufusYuzdesiGenel = "nufus_yuzdesi_genel"
        case ilceler
        case kisaBilgi = "kisa_bilgi"
    }
}

enum Bolge: String, Codable {
    case akdeniz = "Akdeniz"
    case bolgeDoğuAnadolu = "Doğu Anadolu "
    case doğuAnadolu = "Doğu Anadolu"
    case ege = "Ege"
    case güneydoğuAnadolu = "Güneydoğu Anadolu"
    case i̇çAnadolu = "İç Anadolu"
    case karadeniz = "Karadeniz"
    case marmara = "Marmara"
}

// MARK: - Ilceler
struct Ilceler: Codable {
    let plakaKodu, ilceKodu, ilAdi, ilceAdi: String
    let nufus, erkekNufus, kadinNufus: String

    enum CodingKeys: String, CodingKey {
        case plakaKodu = "plaka_kodu"
        case ilceKodu = "ilce_kodu"
        case ilAdi = "il_adi"
        case ilceAdi = "ilce_adi"
        case nufus
        case erkekNufus = "erkek_nufus"
        case kadinNufus = "kadin_nufus"
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img