如何将 [String:Int] 转换为 [String:Double]
How to cast [String:Int] to [String:Double]
我有一个函数需要 [String:Double]
格式的字典,但我的数据目前是 [String:Int]
.
我非常希望能够使用一行代码转换字典,例如
let doubles = ints as! [String:Double]
但这似乎不起作用,给出了错误信息Cannot convert value of type '[String : Int]' to type '[String : Double]' in coercion
虽然我确信我可以解决这个问题,但我真的很想了解
- 为什么我不能将“[String : Int]”转换为“[String : Double]”
- 我应该使用什么方法(也许像
.map
这样的方法有用?)
非常感谢!
类似这样的方法可能有效:
for (key, value) in dictionary {
newDictionary[key] = Double(value)
}
我有一个函数需要 [String:Double]
格式的字典,但我的数据目前是 [String:Int]
.
我非常希望能够使用一行代码转换字典,例如
let doubles = ints as! [String:Double]
但这似乎不起作用,给出了错误信息Cannot convert value of type '[String : Int]' to type '[String : Double]' in coercion
虽然我确信我可以解决这个问题,但我真的很想了解
- 为什么我不能将“[String : Int]”转换为“[String : Double]”
- 我应该使用什么方法(也许像
.map
这样的方法有用?)
非常感谢!
类似这样的方法可能有效:
for (key, value) in dictionary {
newDictionary[key] = Double(value)
}