Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial 1 #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions Tutorial/Tutorial.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import Foundation
...
]
*/
struct Cham: Codable {
var key: String
var name: String
var tags: [String]
}

let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json")

/*
Expand All @@ -27,6 +33,25 @@ let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFi
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] = []
print(names)
//if let champs = champs as? [[String: Any]], let selectedIndexes = selectedIndexes as? [Int] {
//
// let result = selectedIndexes.map { (index) -> [String] in
// champs.filter { (dict) -> Bool in
// return (dict["key"] as? String)?.elementsEqual("\(index)") ?? false
// }.map({ cham in (cham["name"] as? String ?? "") })
// }
// print(result)
//}

let decoder = JSONDecoder()
if let champsData = champsData,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwrapping(i.e. if let, guard let 등)을 최소화 하여 코드를 작성해보시면 더 좋은 인지복잡도점수를 얻을 것 같습니다.
훈련의 차원에서

  1. 현재 인지복잡도 점수를 계산해보고
  2. 점수를 1점씩 줄이는 시도를 계속 하면
    도움될 것 같습니다.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

계산이 귀찮으면 if, for등으로 시작되는 코드블럭을 최소화 한다고 생각하셔도 됩니다.

let champsResponse = try? decoder.decode([Cham].self, from: champsData),
let selectedIndexes = selectedIndexes as? [Int] {

let name = selectedIndexes.compactMap({ "\($0)" })
.flatMap { (index) -> [Cham] in champsResponse.filter({ $0.key.elementsEqual(index) }) }
.map({ $0.name })

print(name)

}