在[String:String]中转换[(key:String,value:String)] [英] convert [(key: String, value: String)] in [String:String]
本文介绍了在[String:String]中转换[(key:String,value:String)]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在[String:String]中转换[(键:字符串,值:字符串)]可以吗?如果是的话,我怎么做?谢谢
I want convert [(key: String, value: String)] in [String:String] is possible to do ? If yes how i make ? thanks
var KeyValuePair: [(key: String, value: String)] = [(key: "2017 01 04", value: "143.65"), (key: "2017 01 05", value: "140.78"), (key: "2017 01 06", value: "150.23")]
在
var dictionary: [String:String] = ["2017 01 04":"143.65", "2017 01 05":"140.78", "2017 01 06":"150.23"]
推荐答案
您只需要遍历元组数组,并使用元组的值设置字典的键值对.
You just need to iterate through the array of tuples and set the key-value pair of your dictionary with the values of the tuple.
var keyValuePairs: [(key: String, value: String)] = [(key: "2017 01 04", value: "143.65"), (key: "2017 01 05", value: "140.78"), (key: "2017 01 06", value: "150.23")]
var dictionary = [String:String]()
keyValuePairs.forEach{
dictionary[$0.0] = $0.1
//since you have named tuples, you could also write dictionary[$0.key] = $0.value
}
print(dictionary)
请确保您遵循Swift命名约定,该约定对于变量名来说是小写字母.
Please make sure you conform to the Swift naming convention, which is lower-camelcase for variable names.
这篇关于在[String:String]中转换[(key:String,value:String)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文