Home > Net >  Unable to get JSON response selected city id from dropdown in swift
Unable to get JSON response selected city id from dropdown in swift

Time:11-23

JSON response like this

{
"result": {
    "cities": [
        {
            "id": 1,
            "city": "North and Middle Andaman",
            "state_id": 32
        },
        {
            "id": 2,
            "city": "South Andaman",
            "state_id": 32
        },
        {
            "id": 3,
            "city": "Nicobar",
            "state_id": 32
        },
        {
            "id": 330,
            "city": "Mumbai suburban",
            "state_id": 12
        },

I am showing cities in textfield with TextFieldEditingChanged and dropdown

code: with this code i can show city list in dropdown and got selected city in textfield but i need selected city id as well.. which i am unable to get

if i print print(item, self?.cityId). here Mumbai suburban id is 330 but got dropdown index 2... how do i get selected city id from dropdown, please guide me

o/p

Mumbai suburban Optional(2)

@IBAction func cityTextFieldEditingChanged(_ sender: UITextField) {
  
    var tempArray = [String]()

    if sender.text?.count != 0 {

        for eachData in cityResultDict?.cities ?? []{

            if let wordToSearch = sender.text{
                let range = eachData.city?.lowercased().range(of: wordToSearch, options: .caseInsensitive, range: nil, locale: nil)

                if range != nil {
                    tempArray.append(eachData.city ?? "")
                }
            }
        }
    }
    showDropDown(with: tempArray, from: sender) { [weak self] (item, index) in
        self?.locationTextField.text = item
        self?.cityId = cityResultDict?.cities?[index].id ?? 0

        print(item, self?.cityId)
    }
} 

CodePudding user response:

if i print print(item, self?.cityId). here Mumbai suburban id is 330 but got dropdown index 2..

Because you are making a new array which is tempArray matching with your search condition when you showDropDown.

I don't know how you implement your showDropDown. But this is a key for your code

Code will be like this

var tempArray = [String]()
var tempCityId = [String]() // I not recommend slipt like this which one is for name city and one is for your id

if sender.text?.count != 0 {

    for eachData in cityResultDict?.cities ?? []{

        if let wordToSearch = sender.text{
            let range = eachData.city?.lowercased().range(of: wordToSearch, options: .caseInsensitive, range: nil, locale: nil)

            if range != nil {
                tempArray.append(eachData.city ?? "")
                // make new array to store your cityId too
                tempId.append(eachData.cityId ?? "")
            }
        }
    }
}
showDropDown(with: tempArray, from: sender) { [weak self] (item, index) in
    self?.locationTextField.text = item
    self?.cityId = tempCityId[index].id ?? 0 // get the id form your tempCityId too

    print(item, self?.cityId)
}

Notice: This way I don't recommend. You can make a new struct which store all your reaching City or a struct only store your cityName and cityId which easier for you.

I build this way just for easier for you to realize and because I don't know how you build your showDropDown

  • Related