Last active
December 14, 2015 22:04
-
-
Save joemasilotti/40309e18af962b59df4d to your computer and use it in GitHub Desktop.
Swift Array to Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
struct Option { | |
var id: UInt | |
var name: String | |
} | |
func combine(options: [Option]) -> [UInt: String] { | |
var combinedOptions = [UInt: String]() | |
for option in options { | |
combinedOptions[option.id] = option.name | |
} | |
return combinedOptions | |
} | |
let option1 = Option(id: 1, name: "One") | |
let option2 = Option(id: 2, name: "Two") | |
let option3 = Option(id: 3, name: "Three") | |
combine([option1, option2, option3]) // [2: "Two", 3: "Three", 1: "One"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can do something like this:
I typed this in the phone with autocorrect on, may not compile out of the box!