-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Motivation - Section 2.1. Create a Landmark Model ### Key Changes - Section 2.1. Create a Landmark Model 예제 코드 및 주석 추가 ### To Reviewers - 없음
- Loading branch information
1 parent
e3dcb5a
commit a53bd24
Showing
10 changed files
with
390 additions
and
79 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Binary file modified
BIN
+914 Bytes
(100%)
...proj/project.xcworkspace/xcuserdata/raymondkim.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Landmark.swift | ||
// tutorial | ||
// | ||
// Created by 김제필 on 8/11/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI // 2.1.5. imageName 변수 추가를 위한 프레임워크 호출 | ||
import CoreLocation // 2.1.7. computed property인 locationCoordinates를 위한 라이브러리 | ||
|
||
// 2.1.3. Landmark 구조체(structure) 정의 | ||
// 몇 가지 필요한 데이터가 이미 landmarkData.json 파일에 담겨 있는데, 이 json파일에 담겨 있는 데이터의 항목명과 동일하게 항목을 작성한다. | ||
// Codable 옵션은 데이터를 Landmark 구조체와 json 파일 간 데이터 전송을 위한 것 | ||
// 나중에 Codable 프로토콜의 Decodable 컴포넌트가 사용됨: 파일에서 데이터를 읽기 위한 것. | ||
struct Landmark: Hashable, Codable { | ||
var id: Int | ||
var name: String | ||
var park: String | ||
var state: String | ||
var description: String | ||
|
||
// 2.1.5. imageName 변수 추가: Assets에 등록된 이미지 파일의 파일명을 읽어오기 위한 것 | ||
private var imageName: String | ||
var image: Image { | ||
Image(imageName) | ||
} | ||
|
||
// 2.1.6. 랜드마크 위치 정보를 담기 위한 변수 coordinates 선언 | ||
private var coordinates: Coordinates | ||
|
||
// 2.1.7. locationCoordinate: computed property -> var 로 선언 | ||
// computed property는 다른 속성을 기반으로 해당 속성 값이 결정됨. 즉, 메모리 공간을 가지지 않음. | ||
// 선언 시점에 기본값을 저장하지 않으므로 형식 추론이 불가능하며, 따라서 반드시 자료형(Type)을 같이 선언해야 함 | ||
var locationCoordinates: CLLocationCoordinate2D { | ||
CLLocationCoordinate2D(latitude: coordinates.latitude, | ||
longitude: coordinates.longitude) | ||
} | ||
|
||
|
||
struct Coordinates: Hashable, Codable { | ||
var latitude: Double | ||
var longitude: Double | ||
} | ||
} | ||
|
||
|
||
// 여기까지 코딩이 됐으면 이제 ModelData.swift 파일을 작성해야 한다. | ||
// ModelData.swift 파일을 생성하자. 일반 Swift 파일이다. |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// ModelData.swift | ||
// tutorial | ||
// | ||
// Created by 김제필 on 8/11/22. | ||
// | ||
|
||
import Foundation | ||
|
||
// 2.1.10 landmarkData.json의 데이터를 받아올 어레이 선언 | ||
var landmarks: [Landmark] = load("landmarkData.json") | ||
|
||
// 2.1.9. 애플리케이션의 메인 번들에서 지정된 이름의 json 데이터를 가져오는 메서드 생성 | ||
// load 메서드는 Codable 프로토콜의 구성요소인 Decodable 프로토콜의 반환 타입을 따른다. | ||
func load<T: Decodable>(_ filename: String) -> T { | ||
let data: Data | ||
|
||
guard let file = Bundle.main.url(forResource: filename, | ||
withExtension: nil) | ||
else { | ||
fatalError("Canot find \(filename) in main bundle.") | ||
} | ||
|
||
do { | ||
data = try Data(contentsOf: file) | ||
} catch { | ||
fatalError("Cannot load \(filename) from main bundle:\n\(error)") | ||
} | ||
|
||
do { | ||
let decoder = JSONDecoder() | ||
|
||
return try decoder.decode(T.self, from: data) | ||
} catch { | ||
fatalError("Cannot parse \(filename) as \(T.self):\n\(error)") | ||
} | ||
} | ||
|
||
// 여기까지 작업이 끝났다면 디렉토리를 정리하자. | ||
// 어떤 디렉토리에 어떤 파일을 배치할 것인지는 공식 문서를 보면 나옴. |
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// MainContentView.swift | ||
// tutorial | ||
// | ||
// Created by 김제필 on 8/10/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
// View 프로토콜 렌더링을 위한 구조체(Structure) | ||
struct MainContentView: View { | ||
var body: some View { | ||
// 1.6.2. 기존 VSTack을 감쌀 새 VStack 삽입 | ||
VStack { | ||
// 1.6.3. MapView 가져오기, 6.4. 리이브 프리뷰로 확인 | ||
MapView() | ||
.ignoresSafeArea(edges: .top) // 1.6.8. safe area 무시 옵션 추가 | ||
.frame(height: 300) | ||
|
||
CircleImage() // 1.6.5. CircleImage 뷰 추가 | ||
.offset(y: -130) // 1.6.6. y축 offset 값 -130으로 설정 -> MapView 위에 CircleImage를 겹쳐 표시 | ||
.padding(.bottom, -130) // 1.6.6. 하단 padding값도 -130으로 설정 -> 아래 텍스트 뷰와의 거리 조절 | ||
|
||
// 1.3.1., 1.3.2. VStack 추가 | ||
VStack(alignment: .leading) { // 1.3.5. alignment 옵션 추가 | ||
Text("Turtle Rock") | ||
.font(.title) | ||
|
||
// 1.3.6. HSTack 추가 | ||
HStack { | ||
Text("Joshua Tree National Park") // 1.3.3. TextView의 placeholder 변경 | ||
// .font(.subheadline) // 1.3.4. 폰트 변경 | ||
|
||
Spacer() // 1.3.8. Spacer 추가 | ||
|
||
Text("California") // 1.3.7. TextView 추가 | ||
// .font(.subheadline) | ||
} | ||
.font(.subheadline) // 1.6.10. 폰트 설정 이동 | ||
.foregroundColor(.secondary) // 1.6.10. 폰트 색상 변경 | ||
|
||
Divider() // 1.6.9. divider 및 기타 정보를 표시하는 Text 추가 | ||
|
||
Text("About Turtle Rock") | ||
.font(.title2) | ||
|
||
Text("Descriptive text goes here.") | ||
} | ||
.padding() // 1.3.9. 패딩 추가 | ||
|
||
Spacer() // 1.6.7. Spacer 추가 -> 전체 오브젝트를 화면 상단으로 배치 | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
struct MainContentView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
MainContentView() | ||
} | ||
} | ||
|
||
// 이제 CircleImage.swift 파일을 작성한다. | ||
// CircleImage.swift 파일은 SwiftUIView를 위한 파일이다. |
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