I have a case where I want to store values in a dictionary, with the keys being a sequence of strings.
In Python, for example, I'd just use a tuple:
foo = {('a', 'b', 'c'): 10}
In Swift, though, dictionary keys must be Hashable, and tuples (being structs) aren't. Neither are arrays. (I know this, and therefore, this is different from the "In Swift can I use a tuple as the key in a dictionary?" question. The answer to that is clearly no.)
I could write my own class ListOfStrings: Hashable
class that's just a [String]
, with its own hash code, but that seems like something I shouldn't be doing, in a high-level language.
I could use NSDictionary/NSArray, but that also seems like an awkward solution. Plus, the hash code of an NSArray is simply its length, which can't be good for performance.
Is there a better way to do this in Swift?