From ebcebd04def11fb346fac5367767e7360fc1d010 Mon Sep 17 00:00:00 2001 From: denis Date: Tue, 12 Nov 2019 14:34:52 +0900 Subject: [PATCH 1/3] =?UTF-8?q?-=201=EC=B0=A8=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tutorial/Tutorial.playground/Contents.swift | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 9b0d9f6..b6e1e3d 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -24,9 +24,20 @@ let selectedIndexesFilePath = Bundle.main.path(forResource: "selectedIndexes", o 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: []) +let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) as! [[String : Any]] +let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) as! [Int] -// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 -let names: [String] = [] -print(names) +//// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 +//champs.filter { +// let key = $0["key"] +// +// return true +//} +champs.filter { + let keyNumer = Int($0["key"] as! String) + let selectedIndexList = selectedIndexes.filter { return $0 == keyNumer } + return selectedIndexList.count > 0 && keyNumer == selectedIndexList[0] +} +.forEach { + print($0["name"]!) +} From 85ec27f9c78623d5b8c01a90228d991812b92a16 Mon Sep 17 00:00:00 2001 From: denis Date: Tue, 12 Nov 2019 14:39:12 +0900 Subject: [PATCH 2/3] =?UTF-8?q?2=EB=B2=88=EC=A7=B8=20=EB=B0=A9=EB=B2=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tutorial/Tutorial.playground/Contents.swift | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index b6e1e3d..c85f1e4 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -28,11 +28,7 @@ let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) as let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) as! [Int] //// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 -//champs.filter { -// let key = $0["key"] -// -// return true -//} +/// 1번쨰 방법 champs.filter { let keyNumer = Int($0["key"] as! String) let selectedIndexList = selectedIndexes.filter { return $0 == keyNumer } @@ -41,3 +37,14 @@ champs.filter { .forEach { print($0["name"]!) } + +/// 2번쨰 방법 +selectedIndexes.forEach { + let index = "\($0)" + champs.filter { + return index == $0["key"] as! String + } + .forEach { + print($0["name"]!) + } +} From 7613199550a8abb22e60b18df50231820de842e4 Mon Sep 17 00:00:00 2001 From: denis Date: Tue, 12 Nov 2019 14:48:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EB=B0=A9=EC=96=B4=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tutorial/Tutorial.playground/Contents.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index c85f1e4..d59f304 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -24,24 +24,27 @@ let selectedIndexesFilePath = Bundle.main.path(forResource: "selectedIndexes", o let champsData = FileManager.default.contents(atPath: champsFilePath!) let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFilePath!) -let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) as! [[String : Any]] -let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) as! [Int] +let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) as? [[String : Any]] +let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) as? [Int] //// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 /// 1번쨰 방법 -champs.filter { +champs?.filter { let keyNumer = Int($0["key"] as! String) - let selectedIndexList = selectedIndexes.filter { return $0 == keyNumer } - return selectedIndexList.count > 0 && keyNumer == selectedIndexList[0] + guard let _selectedIndexList = selectedIndexes?.filter({ return $0 == keyNumer }), + _selectedIndexList.count > 0 else { + return false + } + return keyNumer == _selectedIndexList[0] } .forEach { print($0["name"]!) } /// 2번쨰 방법 -selectedIndexes.forEach { +selectedIndexes?.forEach { let index = "\($0)" - champs.filter { + champs?.filter { return index == $0["key"] as! String } .forEach {