diff --git a/Tutorial/Tutorial.playground/Contents.swift b/Tutorial/Tutorial.playground/Contents.swift index 9b0d9f6..d59f304 100644 --- a/Tutorial/Tutorial.playground/Contents.swift +++ b/Tutorial/Tutorial.playground/Contents.swift @@ -24,9 +24,30 @@ 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)을 나열하라 +/// 1번쨰 방법 +champs?.filter { + let keyNumer = Int($0["key"] as! String) + 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 { + let index = "\($0)" + champs?.filter { + return index == $0["key"] as! String + } + .forEach { + print($0["name"]!) + } +}