Searching dictionary values instead of dictionary keys, in my opinion, is a fairly common and reasonable operation to perform on a dictionary. For instance, if I had some dictionary contacts
that held people's names as keys and their phone numbers as values, I'd most commonly want to look up people's phone numbers, but it's not inconceivable that I'd often want to look up the name for a given phone number as well. It can be done, such as with the following implementation:
myDictionary.first(where: {$1==someValue})?.key
But the readability and conciseness here is really quite low. We could also initialize some dictionary with the values and the keys swapped, then search that:
var swappedDictionary = Dictionary(grouping: myDictionary.keys, by: {myDictionary[$0]! })
swappedDictionary[someValue]! //access key to correspond with value
but while this makes it much more readable and concise to access after the swapped dictionary is created, the creation of the swapped dictionary itself is even less readable than the above, and it won't update if elements are added/removed/modified in the original dictionary. Ultimately, the best solutions when this problem is to implement one of these as an extension of Collection, which suggests to me that firstKey(withValue:)
should really just be an included method in the first place. Some kind of allKeys(withValue:)
might be warranted as a possible substitute instead.