From 50d164eaa6069f23e8f94fe4fe4b874a1faa782b Mon Sep 17 00:00:00 2001 From: Jinwoo Kim Date: Tue, 12 Nov 2019 13:48:59 +0900 Subject: [PATCH 1/6] Solve --- Tutorial/Tutorial.playground/Contents.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 9b0d9f6..4a35e8b 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -22,11 +22,23 @@ let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json") let selectedIndexesFilePath = Bundle.main.path(forResource: "selectedIndexes", ofType: "json") let champsData = FileManager.default.contents(atPath: champsFilePath!) + let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFilePath!) 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] = [] +var names: [String] = [] + +let champsArray = champs as! [[String : Any]] + +let indexesArray = selectedIndexes as! [Int] + +indexesArray.forEach { key in + let filteredChamp = champsArray.filter { ($0["key"] as! String) == String(key) }.first! + let champName = filteredChamp["name"] as! String + names.append(champName) +} + print(names) From 372db436762b044f1ecc46f9337563c3b676d6f4 Mon Sep 17 00:00:00 2001 From: Jinwoo Kim Date: Tue, 12 Nov 2019 13:48:59 +0900 Subject: [PATCH 2/6] Solve --- Tutorial/Tutorial.playground/Contents.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 9b0d9f6..4a35e8b 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -22,11 +22,23 @@ let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json") let selectedIndexesFilePath = Bundle.main.path(forResource: "selectedIndexes", ofType: "json") let champsData = FileManager.default.contents(atPath: champsFilePath!) + let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFilePath!) 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] = [] +var names: [String] = [] + +let champsArray = champs as! [[String : Any]] + +let indexesArray = selectedIndexes as! [Int] + +indexesArray.forEach { key in + let filteredChamp = champsArray.filter { ($0["key"] as! String) == String(key) }.first! + let champName = filteredChamp["name"] as! String + names.append(champName) +} + print(names) From 76bd29608a7d3911e3937a0abb6ce7b564c98faa Mon Sep 17 00:00:00 2001 From: Jinwoo Kim Date: Tue, 12 Nov 2019 13:56:22 +0900 Subject: [PATCH 3/6] Solve --- Tutorial/Tutorial.playground/Contents.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 4a35e8b..f5e050b 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -36,9 +36,13 @@ let champsArray = champs as! [[String : Any]] let indexesArray = selectedIndexes as! [Int] indexesArray.forEach { key in - let filteredChamp = champsArray.filter { ($0["key"] as! String) == String(key) }.first! - let champName = filteredChamp["name"] as! String - names.append(champName) + + let filteredChamp = champsArray.filter { json in + (json["key"] as! String) == String(key) } + .first! + let champName = filteredChamp["name"] as! String + + names.append(champName) } -print(names) +print("Names: \(names)") From 774990f3a0aecc57c6b7f152ee5d85571bbafde5 Mon Sep 17 00:00:00 2001 From: Jinwoo Kim Date: Tue, 12 Nov 2019 14:06:22 +0900 Subject: [PATCH 4/6] Solve --- Tutorial/Tutorial.playground/Contents.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index b7f756f..a7c8bf8 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -35,11 +35,11 @@ let champsArray = champs as! [[String : Any]] let indexesArray = selectedIndexes as! [Int] -indexesArray.forEach { key in +indexesArray.forEach { value in let filteredChamp = champsArray.filter { json in - (json["key"] as! String) == String(key) } + (json["key"] as! String) == String(value) } .first! let champName = filteredChamp["name"] as! String From 57ff599a6adae2d4e77ff419a9d3eee7166aecd6 Mon Sep 17 00:00:00 2001 From: Jinwoo Kim Date: Tue, 12 Nov 2019 14:32:11 +0900 Subject: [PATCH 5/6] refactor: Solved --- Tutorial/Tutorial.playground/Contents.swift | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index a7c8bf8..e297283 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -31,19 +31,22 @@ let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData // TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 var names: [String] = [] -let champsArray = champs as! [[String : Any]] - -let indexesArray = selectedIndexes as! [Int] +let champsJson = champs as! [[String : Any]] +let indexesArray: [String] = (selectedIndexes as! [Int]).map { number in + number.description +} indexesArray.forEach { value in - - let filteredChamp = champsArray.filter { json in - (json["key"] as! String) == String(value) } - .first! - let champName = filteredChamp["name"] as! String + let filteredChamps = champsJson.filter { dict in + (dict["key"] as! String) == value + } + + guard + let filteredChamp = filteredChamps.first, + let champName = filteredChamp["name"] as? String else { return } names.append(champName) } -print("Names: \(names)") +print("Result: \(names)") From d277204fc124f7e6dd5f6865111f1caafafe86ec Mon Sep 17 00:00:00 2001 From: Jinwoo Kim Date: Tue, 12 Nov 2019 14:35:20 +0900 Subject: [PATCH 6/6] refactor again --- Tutorial/Tutorial.playground/Contents.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index e297283..891a5b6 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -39,7 +39,8 @@ let indexesArray: [String] = (selectedIndexes as! [Int]).map { number in indexesArray.forEach { value in let filteredChamps = champsJson.filter { dict in - (dict["key"] as! String) == value + guard let dictValue = dict["key"] as? String else { return false } + return dictValue == value } guard