lukaskubanek / OrderedDictionary

Ordered dictionary data structure implementation in Swift
MIT License
201 stars 63 forks source link

how to convert [String:[[String:AnyObject]]] to OrderedDictionary type? #56

Closed yoonjason closed 4 years ago

yoonjason commented 4 years ago

how to convert [String:[[String:AnyObject]]] to OrderedDictionary type?

let orderedDictionary: OrderedDictionary<String, Any> = (self.dic as? OrderedDictionary)!

warning message Cast from '[String : [[String : Any]]]' to unrelated type 'OrderedDictionary<String, Any>' always fails

lukaskubanek commented 4 years ago

You cannot cast a regular Dictionary to OrderedDictionary. You can use either a dictionary literal or an initializer on OrderedDictionary which takes a regular Dictionary and specify the ordering closure (see below).

let dictionary: [String:[[String:AnyObject]]] = [
    "A": [],
    "C": [],
    "B": [],
]

let orderedDictionary = OrderedDictionary(unsorted: dictionary) { element1, element2 in
    element1.key < element2.key
}