How to make picker view to display from default country but it is displaying from first but I need it from default country that is united states
but here it is showing from first country but I need it to display from United States can anyone help me how to implement this ?
here are the images shown below here picker view displaying from first country but I need it to display from United States because it is my default selected country
func countryListDownloadJsonwithURL(countryAPI: String) {
let url = NSURL(string: countryAPI)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {
for item in jsonObj! {
let dict = Country(dict: item)
self.countryArray.append(dict)
}
print(self.countryArray)
OperationQueue.main.addOperation({
self.countryPickerView.selectRow(self.i, inComponent: 0, animated: true)
self.countryPickerView.reloadAllComponents()
self.statePickerView.reloadAllComponents()
})
}
}).resume()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int{
if pickerView.tag == 1 {
return 1
}else {
return 1
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView.tag == 1 {
return countryArray.count
}
else {
return countryArray[countrySelectedIndex!].state.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return countryArray[row].country
}
else {
return countryArray[countrySelectedIndex!].state[row].state
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
countryTextField.text = countryArray[row].country
for item in countryArray {
print(item.country)
print(countryArray[row].country)
if item.country == countryArray[row].country {
self.countryId = item.countryId
if item.state.count == 0 {
self.stateTextfield.isHidden = false
self.stateLabel.isHidden = true
self.selectedCountryTextField.isHidden = true
self.imageArrow.isHidden = true
break
}
else {
if let i = countryArray.index(where: { $0.country == countryArray[row].country }) {
countrySelectedIndex = i
}
self.countryId = item.countryId
if self.countryId?.isEmpty == true {
self.countryId = countryTextField.text
}
UserDefaults.standard.set(self.countryId, forKey: "guestCountryId")
self.statePickerView.delegate = self
self.statePickerView.dataSource = self
selectedCountryTextField.inputView = statePickerView
self.statePickerView.reloadAllComponents()
selectedCountryTextField.text = "Select state/province"
self.stateLabel.isHidden = false
self.stateTextfield.isHidden = true
self.selectedCountryTextField.isHidden = false
self.imageArrow.isHidden = false
break
}
}
else {
self.stateTextfield.isHidden = false
self.stateLabel.isHidden = true
self.selectedCountryTextField.isHidden = true
self.imageArrow.isHidden = true
}
}
}
else {
self.selectedCountryTextField.text = countryArray[countrySelectedIndex!].state[row].state
self.stateCode = countryArray[countrySelectedIndex!].state[row].stateValue as! Int
print(self.stateCode)
UserDefaults.standard.set(self.stateCode, forKey: "statecode")
}
}