From 75bc1b4e27b6f28336095bb4a220d56900577a2a Mon Sep 17 00:00:00 2001 From: Jino0910 Date: Tue, 12 Nov 2019 13:52:16 +0900 Subject: [PATCH 1/2] log champ --- 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..5d80ddc 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -28,5 +28,17 @@ 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] = [] + +if let champsArray = champs as? Array, let selectedIndexesArray = selectedIndexes as? Array { + + selectedIndexesArray.forEach { (index) in + if let index = index as? Int, champsArray.count >= index, let champ = champsArray[index] as? Dictionary, let name = champ["name"] as? String { + names.append(name) + } + } +} + + + print(names) From d230829974e98a19e3a999b3a4aefb6ab50ef82c Mon Sep 17 00:00:00 2001 From: Jino0910 Date: Tue, 12 Nov 2019 14:31:02 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EA=B3=A0=EA=B3=84=ED=95=A8=EC=88=98?= =?UTF-8?q?=ED=99=9C=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tutorial/Tutorial.playground/Contents.swift | 43 ++++++++++++++++---- Tutorial/Tutorial2.playground/Contents.swift | 26 ++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 5d80ddc..2c8a145 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -28,17 +28,44 @@ let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) // TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 -var names: [String] = [] +//var names: [String] = [] +// +//if let champsArray = champs as? Array, let selectedIndexesArray = selectedIndexes as? Array { +// +// selectedIndexesArray.forEach { (index) in +// if let index = index as? Int, champsArray.count >= index, let champ = champsArray[index] as? Dictionary, let name = champ["name"] as? String { +// names.append(name) +// } +// } +//} +//print(names) -if let champsArray = champs as? Array, let selectedIndexesArray = selectedIndexes as? Array { - selectedIndexesArray.forEach { (index) in - if let index = index as? Int, champsArray.count >= index, let champ = champsArray[index] as? Dictionary, let name = champ["name"] as? String { - names.append(name) +var names2: [String] = [] + +if let champsArray = champs as? Array>, let selectedIndexesArray = selectedIndexes as? Array { + + print( + selectedIndexesArray.compactMap { (index) -> String? in + guard let index = index as? Int else { return nil } + guard champsArray.count > index else { return nil } + return String(index) + } + .compactMap ({ (key) -> Dictionary? in + return champsArray.first(where: { (champ) -> Bool in + guard let champKey = champ["key"] as? String else { return false } + return champKey == key + }) + }).compactMap { (obj) -> String? in + + return obj["name"] as? String } - } + + ) + + + } +//print(names2) - -print(names) diff --git a/Tutorial/Tutorial2.playground/Contents.swift b/Tutorial/Tutorial2.playground/Contents.swift index 9b0d9f6..04edc17 100644 --- a/Tutorial/Tutorial2.playground/Contents.swift +++ b/Tutorial/Tutorial2.playground/Contents.swift @@ -30,3 +30,29 @@ let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData // TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 let names: [String] = [] print(names) + +if let champsArray = champs as? Array>, let selectedIndexesArray = selectedIndexes as? Array { + + print( + selectedIndexesArray.compactMap { (index) -> String? in + guard let index = index as? Int else { return nil } + guard champsArray.count > index else { return nil } + + return String(index) + } + // .flatMap{$0} + .compactMap ({ (key) -> Dictionary? in + return champsArray.first(where: { (champ) -> Bool in + guard let champKey = champ["key"] as? String else { return false } + return champKey == key + }) + }).compactMap { (obj) -> String? in + + return obj["name"] as? String + } + + ) + + + +}