Last active
April 21, 2017 02:47
-
-
Save gonzoooooo/94c14f1a75931c6abb49bb02c5d326e1 to your computer and use it in GitHub Desktop.
Swift 3 で Array から 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
import Foundation | |
struct Person { | |
var number: Int | |
var name: String | |
} | |
let persons = [Person(number: 1, name: "John"), | |
Person(number: 2, name: "Ken"), | |
Person(number: 3, name: "Mike")] | |
let personDic = persons.reduce([Int: Person]()) { (result, person) in | |
var newResult = result | |
newResult[person.number] = person | |
return newResult | |
} | |
let thirdPerson = personDic[3] | |
thirdPerson?.name // Mike |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment