From ab56291a3c12deba10bae630eff10c7b98a9f7cf Mon Sep 17 00:00:00 2001 From: kuu723 Date: Tue, 12 Nov 2019 13:44:45 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EC=B1=94=ED=94=BC=EC=96=B8=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=B0=BE=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tutorial/Tutorial.playground/Contents.swift | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 9b0d9f6..ad8512c 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -10,6 +10,12 @@ import Foundation ... ] */ +struct Cham: Codable { + var key: String + var name: String + var tags: [String] +} + let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json") /* @@ -27,6 +33,17 @@ let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFi let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) -// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 -let names: [String] = [] -print(names) +let decoder = JSONDecoder() +if let champsData = champsData, + let champsResponse = try? decoder.decode([Cham].self, from: champsData), + let selectedIndexes = selectedIndexes as? [Int] { + + let value = selectedIndexes.map({ index in + return champsResponse.first { (cham) -> Bool in + return cham.key == "\(index)" + } + }) + + let names: [String] = value.map({ $0?.name ?? "" }) + print("names: ", names) +} From 59b974f3babaa73e5ab3a53a960fb651ac8446ec Mon Sep 17 00:00:00 2001 From: kuu723 Date: Tue, 12 Nov 2019 14:19:59 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=B2=AB=EB=B2=88=EC=A7=B8=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tutorial/Tutorial.playground/Contents.swift | 22 ++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index ad8512c..cf78396 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -33,17 +33,25 @@ let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFi let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) +//if let champs = champs as? [[String: Any]], let selectedIndexes = selectedIndexes as? [Int] { +// +// let result = selectedIndexes.map { (index) -> [String] in +// champs.filter { (dict) -> Bool in +// return (dict["key"] as? String)?.elementsEqual("\(index)") ?? false +// }.map({ cham in (cham["name"] as? String ?? "") }) +// } +// print(result) +//} + let decoder = JSONDecoder() if let champsData = champsData, let champsResponse = try? decoder.decode([Cham].self, from: champsData), let selectedIndexes = selectedIndexes as? [Int] { - let value = selectedIndexes.map({ index in - return champsResponse.first { (cham) -> Bool in - return cham.key == "\(index)" - } - }) + let name = selectedIndexes.compactMap({ "\($0)" }) + .flatMap { (index) -> [Cham] in champsResponse.filter({ $0.key.elementsEqual(index) }) } + .map({ $0.name }) + + print(name) - let names: [String] = value.map({ $0?.name ?? "" }) - print("names: ", names) }