You either have to add an explicit check (and throw an error or fail
if duplicate keys are given), or add the key/value pairs sequentially
so that duplicate keys are silently overwritten with the new value:
init(_ tupleArgs: [KeyValueType]) {
self.init()
for (key, value) in tupleArgs {
self[key] = value
}
}
extension OrderedDictionary : CustomStringConvertible {
var description: String {
return "[ "
+ tuples.map({ (key, value) in "\(key) : \(value)"}).joinWithSeparator(", ")
+ "]"
}
}
Creating a new (ordered) dictionary by mapping both keys and values
can lead to unexpected results if two keys are mapped to the same
new key. E.g. (with the fixed init method from above):
let od1 = OrderedDictionary(("foo", 1), ("bar", 2), ("FOO", 3))
print(od1) // [ foo : 1, bar : 2, FOO : 3]
let od2 = od1.map { (key, value) in (key.uppercaseString, value) }
print(od2) // [ FOO : 3, BAR : 2]
You have to decide if that is wanted. Alternatively, define a
map()
method which only maps the values but preserves the keys.