Skip to content

Instantly share code, notes, and snippets.

@ericdke
Last active November 3, 2015 18:57
Show Gist options
  • Select an option

  • Save ericdke/2498b400a42e53cf6e2a to your computer and use it in GitHub Desktop.

Select an option

Save ericdke/2498b400a42e53cf6e2a to your computer and use it in GitHub Desktop.
Make a Dictionary from two arrays
extension CollectionType where Generator.Element: Hashable {
func dictionaryWithValues<U>(values: [U]) -> [Generator.Element:U] {
var dict: [Generator.Element:U] = [:]
zip(self, values).forEach { (k, v) in dict[k] = v }
return dict
}
}
let result = ["name", "age"].dictionaryWithValues(["eric", 42])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment