-
Notifications
You must be signed in to change notification settings - Fork 50
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
1차 수정 버전 - 김성훈 #11
Open
Seonghun23
wants to merge
7
commits into
wanbok:master
Choose a base branch
from
Seonghun23:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
1차 수정 버전 - 김성훈 #11
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0dc4e4c
1차 수정 버전
Seonghun23 a13f8fa
두번째 수정
Seonghun23 cbea944
세번째 수정
Seonghun23 d8edd1b
네번째 수정
Seonghun23 ea59b0d
코드 리팩토링
Seonghun23 b4d2e0b
코드 개선 및 익명함수 사용하지 않도록 수정
Seonghun23 06d377e
코드 리팩토링
Seonghun23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,25 @@ 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] = [] | ||
|
||
let champsDict = champs? | ||
.flatMap { [$0["key"] as? String: $0["name"] as? String] } | ||
// 옵셔널 값을 그대로 딕셔너리에 키값으로 넣었다. 지금은 오류가 없으나 추가적인 사이드 이팩트가 어떤게 있을까? | ||
.reduce(into: [:]) { $0[$1.key] = $1.value } | ||
|
||
let names = selectedIndexes? | ||
.compactMap { String($0) } | ||
.compactMap { champsDict?[$0] } | ||
?? [] | ||
|
||
print(names) | ||
|
||
// flatMap과 reduce를 이용하여 딕셔너리 배열을 딕셔너리로 변경하였다. | ||
// 그 결과 결과값을 바로 캐스팅하여 가져올 수 있게 되었다. | ||
// 이전 코드 보단 한층 깔끔한 느낌이다. | ||
// 다만 flatMap으로 튜플로 변경하는 부분의 코드가 가독성이 좋지 않고 지저분하다. | ||
// 저것도 더 깔끔하게 변경할 수 있을지 고민해 봐야 겠다. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 따봉 드립니다. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이곳의 결과물은 Dictionary일까요 Array일까요?
champs -> flatMap -> reduce 로 바뀌는 과정의 타입 변화를 살펴보면 힌트가 있을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flatMap 결과값은 Tuple을 담고 있는 Array입니다.
다만 타입추론으로 reduce를 거치며 이Tuple의 key에 자료형이 champsDict의 키에 자료형과 맞춰지게 되어 결과적으로 champsDict의 자료형은 Optional String을 키값으로 가지는 Dictionary가 되었습니다.
보통은 Codable을 이용하지만, Codable을 사용하지 않고 고계 함수만으로 개선하려 하니 더 고민이었던 것 같습니다.