【问题标题】:Calling dictionary keys using another array使用另一个数组调用字典键
【发布时间】:2016-08-10 16:03:45
【问题描述】:

我有一个 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,但将pokemonData 定义为typeData.key。这样你只需要在一个地方写口袋妖怪,两个都会更新。

标签: arrays swift dictionary uipickerview


【解决方案1】:

只需使用该 typeData Dictionary 即可获取所选键的值。

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

【讨论】:

  • @VeronicaG 检查最新的编辑我只是不匹配标签:)
  • @VeronicaG 如果这回答了您的问题,请接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多