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

고계함수활용 #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 41 additions & 2 deletions Tutorial/Tutorial.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,44 @@ 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)
//var names: [String] = []
//
//if let champsArray = champs as? Array<Any>, let selectedIndexesArray = selectedIndexes as? Array<Any> {
//
// selectedIndexesArray.forEach { (index) in
// if let index = index as? Int, champsArray.count >= index, let champ = champsArray[index] as? Dictionary<String, Any>, let name = champ["name"] as? String {
// names.append(name)
// }
// }
//}
//print(names)


var names2: [String] = []

if let champsArray = champs as? Array<Dictionary<String, Any>>, let selectedIndexesArray = selectedIndexes as? Array<Any> {

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<String, Any>? 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)


26 changes: 26 additions & 0 deletions Tutorial/Tutorial2.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dictionary<String, Any>>, let selectedIndexesArray = selectedIndexes as? Array<Any> {

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<String, Any>? 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
}

)



}