OrderedDictionary can't convert value of type '[String : Double]' to specified type 'OrderedDictionary<Key, Value>'

I’m trying to use OrderedDictionary from Collections and I’m getting the following error when trying to convert a Dictionary to OrderedDictionary.

Cannot convert value of type '[String : Double]' to specified type 'OrderedDictionary<Key, Value>'

let values: [String: Double]

let sortedDict: OrderedDictionary<String, Double> = values

>Solution :

The compiler won’t let you cast it directly. Instead, pass values to the initializer.

let sortedDict: OrderedDictionary<String, Double> = OrderedDictionary(uniqueKeysWithValues: values)

Leave a Reply