[swift-users] Mapping a Dictionary to a Dictionary?

Jens Alfke jens at mooseyard.com
Mon Aug 29 16:52:14 CDT 2016


> On Aug 29, 2016, at 2:07 PM, Zhao Xin <owenzx at gmail.com> wrote:
> 
> I don''t quite understand your question. In Swift, Dictionaries are structs. You can always use `let dict2 = dict1`. 

I’m talking about a function that does for Dictionaries what map() does for Arrays: it transforms every key and value in the input Dictionary (through a caller-provided function), producing a new Dictionary.

You could use this to take a dictionary [String:String] that maps user IDs to product IDs, and produce a dictionary [User:Product].
Or you could invert a dictionary (swapping keys and values.)

—Jens


More information about the swift-users mailing list