title: How to show statesList in dropdown according to country from JSON in Swift
body: <p>My JSON like this:</p>
  <pre><code> country =    (
              {
             country_id = 1;
             name = Afghanistan
            },
              {
             country_id = 2;
             name = Albania;
              },....
              
  
  states =         (
                      {
               &quot;country_id&quot; = 1;
              id = 823;
              &quot;state_name&quot; = Badakhshan;
          },
                      {
              &quot;country_id&quot; = 1;
              id = 824;
              &quot;state_name&quot; = Badghis;
          },......
  </code></pre>
  <p>Here country's and state's <code>country_id</code> is same then i am getting that country's related states</p>
  <pre><code>var countrysArray1 = [String]()
  var statesArray1 = [String]()
  var selectedCountryId: Int?
  
  var countrysArray = [Country]()
  var selectedCountry: Country?
  
  var statesArray = [States]()
  var selectedState: States?
  
  fileprivate var getuserData = GetUserBaseModel(dictionary: NSDictionary()){
  didSet{
  
      for countryData in getuserData?.result?.country ?? []{
          self.countrysArray1.append(countryData.name!)
          
          self.countrysArray.append(Country(dictionary: [&quot;country_id&quot;: countryData.country_id as! Int, &quot;name&quot;: countryData.name])!)
      }
      for statesData in getuserData?.result?.states ?? []{
              self.statesArray1.append(statesData.state_name!)
  
              self.statesArray.append(States(dictionary: [&quot;country_id&quot; : statesData.country_id, &quot;id&quot; : statesData.id, &quot;state_name&quot; : statesData.state_name])!)
       
      }
  </code></pre>
  <p>i am doing all below code because i need to show selected country from dropdown in textfield and its id to be passed in JSON.. thats why i am doing leanghthy code.. if i am wrong pls suggest me correct code</p>
  <pre><code>func assignSelectedCountry(havingTitle: String) {
  for country in self.countrysArray {
      if havingTitle == country.name {
          self.selectedCountry = country
          self.populateData()
          selectedCountryId = self.selectedCountry?.id
      }
  }
  }
  
  func populateData() {
  self.countryTextField.text = self.selectedCountry?.name
  print(&quot;country names and id ...... \(String(describing: self.selectedCountry?.name)) \(String(describing: self.selectedCountry?.id))&quot;)
  }
  
  @IBAction func countryBtn(_ sender: UIButton) {
  showDropDown(with: countrysArray1, from: sender) { [weak self] (item, index) in
      self?.assignSelectedCountry(havingTitle: item)
  }
  
  }
  
  @IBAction func stateBtn(_ sender: Any) {
  showDropDown(with: statesArray1, from: sender as! UIButton) { [weak self] (item, index) in
      
      self?.assignSelectedState(havingTitle: item)
  }
  }
  
  func assignSelectedState(havingTitle: String) {
  for state in self.statesArray {
      if havingTitle == state.state_name {
          self.selectedState = state
          self.populateDataforState()
      }
  }
  }
  
  func populateDataforState() {
  self.stateTextField.text = self.selectedState?.state_name
  }
  </code></pre>
  <p>With the above code i am showing all countrys in dropdown and all states in dropdown.. but i need to show selected country's states in states dropdown</p>
  <p>how to do that please help me</p>
  <p><strong>EDIT:</strong></p>
  <pre><code> @IBAction func countryBtn(_ sender: UIButton) {
  
   for statesData in getuserData?.result?.states ?? []{
          let sId = Int(statesData.country_id ?? &quot;&quot;)
       //   print(&quot;selected countryid \(selectedCountryId) dnjndjkdnknk \(sId)&quot;)
          if sId == selectedCountry?.id{
              self.statesArray1.append(statesData.state_name!)
  
              self.statesArray.append(States(dictionary: [&quot;adn1code&quot; : statesData.adn1code, &quot;code&quot; : statesData.code, &quot;country_id&quot; : statesData.country_id, &quot;id&quot; : statesData.id, &quot;state_name&quot; : statesData.state_name])!)
          }
          }
      }
  </code></pre>
  <p>if i do like this only first time stateslist displaying according to country.. if i tap on <code>countryBtn</code> second time then first time selected country states are coming.. pls do help</p>