From 2f3f7daf1b48e20ce0a7e3a6177eb0e3fb0edae2 Mon Sep 17 00:00:00 2001 From: LeeGwangYong Date: Tue, 12 Nov 2019 13:41:46 +0900 Subject: [PATCH 1/4] Update Contents.swift --- Tutorial/Tutorial.playground/Contents.swift | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 9b0d9f6..54189a2 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -10,6 +10,13 @@ import Foundation ... ] */ + +struct Champ: Decodable { + let key: String + let name: String +} + + let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json") /* @@ -24,9 +31,16 @@ 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 decoder = JSONDecoder() +let champs = try decoder.decode([Champ].self, from: champsData!) +let selectedIndexes = try decoder.decode([Int].self, from: selectedIndexesData!) // TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 -let names: [String] = [] + +let names: [String] = selectedIndexes.map { + String(describing: $0) +}.map { key in + champs.first { $0.key == key }?.name +}.compactMap { $0 } print(names) + From b257ce3c175436ac598deffaf97103fd634154ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=8B?= =?UTF-8?q?=E1=85=AD=E1=86=BC?= Date: Tue, 12 Nov 2019 13:46:40 +0900 Subject: [PATCH 2/4] Replace `map` to `compactMap` --- 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 54189a2..e2f9e32 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -39,8 +39,8 @@ let selectedIndexes = try decoder.decode([Int].self, from: selectedIndexesData!) let names: [String] = selectedIndexes.map { String(describing: $0) -}.map { key in +}.compactMap { key in champs.first { $0.key == key }?.name -}.compactMap { $0 } +} print(names) From 941828dbb8b6ba96477e64744ec071371153a805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=8B?= =?UTF-8?q?=E1=85=AD=E1=86=BC?= Date: Tue, 12 Nov 2019 14:17:34 +0900 Subject: [PATCH 3/4] Use Dictionary --- Tutorial/Tutorial.playground/Contents.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index e2f9e32..b6ba2f1 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -37,10 +37,12 @@ let selectedIndexes = try decoder.decode([Int].self, from: selectedIndexesData!) // TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 -let names: [String] = selectedIndexes.map { +let dictionary = champs.reduce(into: [String: String]()) { + $0[$1.key] = $1.name +} +let names = selectedIndexes.map { String(describing: $0) -}.compactMap { key in - champs.first { $0.key == key }?.name +}.compactMap { + dictionary[$0] } print(names) - From 5d34f1fdc72131be078886c88afef27e3d03adf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=8B?= =?UTF-8?q?=E1=85=AD=E1=86=BC?= Date: Tue, 12 Nov 2019 14:44:27 +0900 Subject: [PATCH 4/4] Implement `uniqueMap` --- Tutorial/Tutorial2.playground/Contents.swift | 50 ++++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/Tutorial/Tutorial2.playground/Contents.swift b/Tutorial/Tutorial2.playground/Contents.swift index 9b0d9f6..cd07a6d 100644 --- a/Tutorial/Tutorial2.playground/Contents.swift +++ b/Tutorial/Tutorial2.playground/Contents.swift @@ -1,32 +1,40 @@ import Foundation -/* - [ - { - "key": "266", - "name": "Aatrox", - ... - }, - ... - ] - */ +struct Champ: Decodable { + let key: String + let name: String +} + + let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json") -/* - [ - 1, - 33, - ... - ] - */ 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: []) +let decoder = JSONDecoder() +let champs = try decoder.decode([Champ].self, from: champsData!) +let selectedIndexes = try decoder.decode([Int?].self, from: selectedIndexesData!) + +extension Array where Element: Equatable { + func uniqueMap() -> Self { + return self.reduce([Element]()) { + return !$0.contains($1) ? ($0 + [$1]) : $0 + } + } +} -// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 -let names: [String] = [] +let names: [String] = selectedIndexes + .uniqueMap() + .compactMap { $0 } + .map { + String(describing: $0) + } + .compactMap { key in + champs.first { $0.key == key }?.name + } print(names) + + +