用于填充pickerView的数组的Swift字典

时间:2022-05-14 15:39:42

I have a dictionary... ["12": "AB", "13": "CD"]

我有一个字典…[" 12 ":“AB”,“13”:“CD”)

I am trying to use this dictionary to populate a pickerView, So i am attempting to split the dictionary into arrays as only AB, CD need to be displayed in the picker but once selected they need to correspond back to their keys.

我正在尝试使用这个字典来填充一个pickerView,因此我试图将字典分割成仅为AB的数组,CD需要在picker中显示,但一旦被选中,它们需要与键对应。

I keep getting a Cannot convert value of type 'LazyMapCollection? to expected argument type [_]

我一直得到一个不能转换价值的类型的LazyMapCollection?对于期望参数类型[_]

newArray = Array(arrayLiteral: myDictionary.keys)

Ive tried changing the type of array, using NSMutableArray etc but This same error keeps popping up.

我尝试过改变数组的类型,使用NSMutableArray等等,但是同样的错误不断出现。

If anyone has a better way of using dictionary and pickers, i would be open to suggestions, or if anyone can help me solve this issue.

如果有谁有更好的方式使用字典和挑选器,我愿意接受建议,如果有人能帮助我解决这个问题。

thanks

谢谢

3 个解决方案

#1


1  

newArray = Array(myDictionary.values)

Your newArray will contain the "AB" and "CD" strings.

您的newArray将包含“AB”和“CD”字符串。

#2


0  

I don't know if this is the most "Swift" way to do it, but this works:

我不知道这是否是最“迅速”的方法,但这是可行的:

var myArray: [String] = []
let dict =  ["12": "AB", "13": "CD"]
for (key, value) in dict {
    myArray.append(value)
}

#3


0  

I suspect that this is a typo. If you want AB and CD, shouldn't you get values instead of keys?

我怀疑这是个错字。如果你想要AB和CD,难道不应该得到值而不是键吗?

let newArray = Array(myDictionary.values)

#1


1  

newArray = Array(myDictionary.values)

Your newArray will contain the "AB" and "CD" strings.

您的newArray将包含“AB”和“CD”字符串。

#2


0  

I don't know if this is the most "Swift" way to do it, but this works:

我不知道这是否是最“迅速”的方法,但这是可行的:

var myArray: [String] = []
let dict =  ["12": "AB", "13": "CD"]
for (key, value) in dict {
    myArray.append(value)
}

#3


0  

I suspect that this is a typo. If you want AB and CD, shouldn't you get values instead of keys?

我怀疑这是个错字。如果你想要AB和CD,难道不应该得到值而不是键吗?

let newArray = Array(myDictionary.values)