Veronica G Veronica G - 3 months ago 8
Swift Question

Calling dictionary keys using another array

I have a

UIPickerView
showing data from an
Array
of keys.

var pokemonData = ["Nidoran", "Clefairy", "Spearow", "Ivysaur", "Gengar", "Arcanine", "Gyarados", "Onix", "Pikachu", "Vaporeon"]


Also I have this
Dictionary
, but i want call each key to it shows in a label whenever i choose something on the picker.

var typeData = [

"Nidoran" : "Type: Poison",
"Clefairy" : "Type: Normal",
"Spearow" : "Type: Normal, Flying",
"Ivysaur" : "Type: Grass, Poison",
"Gengar" : "Type: Ghost, Poison",
"Arcanine" : "Type: Fire",
"Gyarados" : "Type: Water, Flying",
"Onix" : "Type: Rock, Ground",
"Pikachu" : "Type: Electric",
"Vaporeon" : "Type: Water"

]


How can I do that? I use this picker function to change the labels

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

pokeLabel.text = pokemonData[row]

}


My second label is...

@IBOutlet weak var typeLabel: UILabel!

Answer

Just use that typeData Dictionary to get value for selected key.

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
     pokeLabel.text = pokemonData[row]
     typeLabel.text = typeData[pokemonData[row]]
}
Comments