Skip to content

Commit

Permalink
Refactor: Section 2.5. update
Browse files Browse the repository at this point in the history
### Motivation
- Section 2.5. Make the List Dynamic

### Key Changes
- Section 2.5. Make the List Dynamic 예제 코드 및 주석 추가

### To Reviewers
- 없음
  • Loading branch information
garlicvread committed Aug 11, 2022
1 parent 342acc7 commit 85cd835
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Binary file not shown.
4 changes: 3 additions & 1 deletion tutorial/Model/Landmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import CoreLocation // 2.1.7. computed property인 locationCoordinates를 위
// 몇 가지 필요한 데이터가 이미 landmarkData.json 파일에 담겨 있는데, 이 json파일에 담겨 있는 데이터의 항목명과 동일하게 항목을 작성한다.
// Codable 옵션은 데이터를 Landmark 구조체와 json 파일 간 데이터 전송을 위한 것
// 나중에 Codable 프로토콜의 Decodable 컴포넌트가 사용됨: 파일에서 데이터를 읽기 위한 것.
struct Landmark: Hashable, Codable {
struct Landmark: Hashable, Codable, Identifiable { // 2.5.3. Identifiable 프로토콜을 따르도록 코드 재작성 -> 데이터를 읽을 때 id 값을 디코딩할 수 있도록 속성을 추가하는 것

// 2.5.3. Landmark 데이터는 Identifiable 프로토콜 사용에 필요한 id 값이 이미 정의가 돼 있는 상태임 -> 2.5.3.까지 따라왔으면 LandmarkList.swift 파일로 이동
var id: Int
var name: String
var park: String
Expand Down
23 changes: 20 additions & 3 deletions tutorial/Views/LandmarkList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@ import SwiftUI

struct LandmarkList: View {
var body: some View {
List { // 2.4.2. List 객체 내부에 LandmarkRow 인스턴스를 배치한다
LandmarkRow(landmark: landmarks[0])
LandmarkRow(landmark: landmarks[1])
// List { // 2.4.2. List 객체 내부에 LandmarkRow 인스턴스를 배치한다
// LandmarkRow(landmark: landmarks[0])
// LandmarkRow(landmark: landmarks[1])
// }

// MARK: 데이터를 identifiable 하게 만드는 방법 두 가지
// 첫째, 각 요소를 고유하게 식별하도록 하는 키 경로를 갖고 오는 방법.
// 둘째, 데이터 형식이 identifiable 프로토콜을 따르도록 만드는 방법.

// // 여기서는 첫 번째 방식으로 코딩
// List(landmarks, id: \.id) { landmark in // 2.5.1. 하드코딩된 리스트 제거, landmarks 배열의 값을 가져옴
// // List는 indentifialble 데이터에 한해 작동한다.
//
// LandmarkRow(landmark: landmark) // 2.5.2. Closure에서 LndmarkRow를 반환함으로써 동적으로 리스트 생성
// // 2.5.2. 항목까지 따라왔으면 이제 Landmark.swift 파일로 가 보자
// }

// 2.5.4. 두 번째 방식 코딩: 코드가 더 간결해짐
List(landmarks) { landmark in
LandmarkRow(landmark: landmark)
}
}
}
Expand Down

0 comments on commit 85cd835

Please sign in to comment.