Create a Swift Dictionary from an Array

Here is a Gist I posted showing a Swift utility function that enables you to create a Dictionary containing an optional entry for every element in an Array. It uses the reduce function to create one Dictionary from every element in an Array, based on a closure you provide that turns an array element into a key-value tuple.

This code was compiled and tested against Xcode 6 Beta 3.

About Josh Smith

Josh Smith is a musician at heart, and a software developer by obsession. He lives in Arizona, plays classical music on the piano, jazz on the guitar, and enjoys brutally long rides on his bicycle. He has a wonderful wife and thinks that, in general, life is good.
This entry was posted in Swift and tagged , , . Bookmark the permalink.

2 Responses to Create a Swift Dictionary from an Array

  1. Jessy says:

    I don’t think we should be creating this kind of thing for specific structs and classes; instead let’s make this an instance method of a protocol, and extend Array to implement it, as with C#: http://msdn.microsoft.com/en-us/library/bb549277(v=vs.95).aspx

    • Josh Smith says:

      Let me know when you put together a sample of this, I’m curious to see your approach.

Comments are closed.