Is there an equivalent for Swift\'s native Dictionary to [NSDictionary initWithObjects: forKeys:]?
Say I have two arrays with keys and valu
A one-liner, using zip and reduce:
let dict = zip(keys, values).reduce([String:Int]()){ var d = $0; d[$1.0] = $1.1; return d }
You can shorten the reduce expression by defining the + operator for a Dictionary and a tuple:
func +(lhs: [K:V], rhs: (K, V)) -> [K:V] {
var result = lhs
result[rhs.0] = rhs.1
return result
}
let dict = zip(keys, values).reduce([String:Int](), combine: +)