gpt4 book ai didi

arrays - 使用另一个数组调用字典键

转载 作者:行者123 更新时间:2023-11-28 12:43:15 24 4
gpt4 key购买 nike

我有一个 UIPickerView 显示来自 Array 键的数据。

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

我也有这个 Dictionary,但我想在选择器上选择某项时调用它显示在标签中的每个键。

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"

]

我该怎么做?我使用这个选择器功能来更改标签

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

pokeLabel.text = pokemonData[row]

}

我的第二个标签是...

@IBOutlet weak var typeLabel: UILabel!

最佳答案

只需使用 typeData Dictionary 来获取所选键的值。

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

关于arrays - 使用另一个数组调用字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38878449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com