Skip to content

Instantly share code, notes, and snippets.

@masakid
Created July 5, 2015 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masakid/6abc802b2a313c49cdd2 to your computer and use it in GitHub Desktop.
Save masakid/6abc802b2a313c49cdd2 to your computer and use it in GitHub Desktop.
[swift]reduceを使って配列からDictionaryを作る方法
/*
reduceを使って配列からDictionaryを作る方法
url: http://qiita.com/_mpon/items/31d8058715bdc2be2ffa
*/
let queries = ["id=3", "token=abc", "tag=5", "plus=3"]
let params = queries.reduce([String: String]()) { (var dict, q) in
println(dict)
println(q)
let v = q.componentsSeparatedByString("=")
let (key, value) = (v[0], v[1])
dict[key] = value
return dict
}
println(params)
// => ["id": "3", "token": "abc", "tag": "5"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment