I just want a data structure that acts similar to a Dictionary
, but not only can I access the values using the keys, but also access the keys using the values.
So it would be something like this:
let dict: ReversibleDictionary<String, String> =
["1" : "one", "2" : "two", "3" : "three"]
dict.valueFromKey("1") // "one"
dict.keyFromValue("two") // "2"
Is there something like that built-in swift?
If there isn't, how can I create something like this myself?
I tried to create one using 2 NSMutableOrderedSet
s and use a linear search to find the corresponding values. But I think it's too inefficient to use a linear search, which is O(n) complexity. Accessing a dictionary should be O(1) complexity, right?