Skip to content
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
36 changes: 35 additions & 1 deletion Tutorial/Tutorial.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,39 @@ 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] = []
print(names)

struct Champ: Codable{
let key: String
let name: String
}

let champArr = try JSONDecoder().decode([Champ].self, from: champsData!)
let select = try JSONDecoder().decode([Int].self, from: selectedIndexesData!)

//["Tryndamere", "Udyr", "Ahri", "Vayne", "Pyke"]
select.forEach{ index in
champArr
.filter{ "\(index)" == $0.key }
.compactMap{ names.append($0.name) }
}

/*
select.compactMap{ key in
champArr
.first{ $0.key == "\(key)" }
.flatMap{ names.append($0.name) }
}
*/
//위와같은 상황일경우 forEach와 compactMap의 동작은 같은가 성능은 어느것이 좋은가?

//아래같은경우는 select순서대로 검색불가
champArr.compactMap{ champ in
select.filter{ champ.key == "\($0)" }.map{ _ in
names.append( champ.name ) }
}



print(names)