ContentView.swift
//
// ContentView.swift
// JongjuAR
//
// Created by 전민정 on 6/6/22.
//
import SwiftUI
struct ContentView: View {
@ObservedObject var datas = ReadData()
var body: some View {
NavigationView {
List(datas.jongjus) { jongju in
Section(header: Text(jongju.name)) {
ForEach(jongju.routes) { route in
NavigationLink(destination: DownloadGPXView(route: route)) {
Text(route.name)
}
}
}
}
}
}
}
struct DownloadGPXView: View {
var route: Route
init(route: Route) {
self.route = route
}
var body: some View {
List {
ForEach(route.courses) { course in
Text(course.name)
}
}
}
}
class ReadData: ObservableObject {
@Published var jongjus = [Jongju]()
init() {
loadData()
}
func loadData() {
guard let url = Bundle.main.url(forResource: "Backdu", withExtension: "json") else {
print("json not found...")
return
}
let data = try? Data(contentsOf: url)
do {
let jongjus = try JSONDecoder().decode([Jongju].self, from: data!)
self.jongjus = jongjus
} catch {
print(error)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Model.swift
//
// Model.swift
// JongjuAR
//
// Created by 전민정 on 6/8/22.
//
import Foundation
struct Course : Codable, Identifiable {
enum CodingKeys: CodingKey {
case num
case name
case mmap
case gpx
}
var id = UUID()
var num: Int
var name: String
var mmap: String
var gpx: String
}
struct Route : Codable, Identifiable {
enum CodingKeys: CodingKey {
case num
case name
case courseType
case courseCnt
case courses
}
var id = UUID()
var num: Int
var name: String
var courseType: String
var courseCnt: Int
var courses: [Course]
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
num = try values.decode(Int.self, forKey: .num)
name = try values.decode(String.self, forKey: .name)
courseType = try values.decode(String.self, forKey: .courseType)
courseCnt = try values.decode(Int.self, forKey: .courseCnt)
courses = try values.decode([Course].self, forKey: .courses)
}
}
struct Jongju : Identifiable, Codable {
enum CodingKeys: CodingKey {
case name
case routes
}
var id = UUID()
var name: String // 백두대간
var routes: [Route]
}
Backdu.json 일부
[{
"name": "백두대간",
"routes": [
{
"num": 1,
"name": "지리산권",
"courseType": "종주",
"courseCnt": 7,
"courses": [
{
"num": 1,
"name": "천왕봉-세석산장",
"mmap": "",
"gpx": "백두대간_지리산권_종주_1코스.gpx"
},
{
"num": 2,
"name": "세석산장-토끼봉",
"mmap": "",
"gpx": "백두대간_지리산권_종주_2코스.gpx"
},
{
"num": 3,
"name": "토끼봉-헬기장",
"mmap": "",
"gpx": "백두대간_지리산권_종주_3코스.gpx"
},
References
https://developer.mozilla.org/ko/docs/Learn/JavaScript/Objects/JSON
JSON으로 작업하기 - Web 개발 학습하기 | MDN
JavaScript Object Notation (JSON)은 Javascript 객체 문법으로 구조화된 데이터를 표현하기 위한 문자 기반의 표준 포맷입니다. 웹 어플리케이션에서 데이터를 전송할 때 일반적으로 사용합니다(서버에서
developer.mozilla.org
https://jinnify.tistory.com/71
[Swift] Codable - Decoding 방법
이전 포스트는 Codable의 Encodable에 대해 알아 보았습니다. 이번 포스트는 Codable의 Decodable에 대해 알아보도록 하겠습니다. Decodable JSON 데이터를 디코딩 하는 방법에 대해 알아보도록 하겠습니다. De
jinnify.tistory.com
How do I make an enum Decodable in swift 4?
enum PostType: Decodable { init(from decoder: Decoder) throws { // What do i put here? } case Image enum CodingKeys: String, CodingKey { case image } } What ...
stackoverflow.com
https://discourse.codershigh.com/t/codable/454
Codable 관련 질문 드립니다
안녕하세요. codable과 관련하여 질문이 있어 글을 올립니다. enum타입에 Codable을 선언해주었더니 ‘does not conform to protocol ‘Decodable’’ ‘does not conform to protocol ‘Encodable’’ 이 뜨면서 encode와 init
discourse.codershigh.com
https://medium.com/@nutanbhogendrasharma/read-json-data-from-file-system-in-swiftui-d054662000e
Read JSON Data From File System in SwiftUI Mobile App
In this blog we are going to read json data from file system and display data in list view.
medium.com
https://www.hohyeonmoon.com/blog/swiftui-tutorial-navigation/
SwiftUI NavigationView 사용하기 | Hohyeon Moon
Hohyeon Moon iOS developer. Code for a happier life. Resume
www.hohyeonmoon.com
'개발 > JongjuAR' 카테고리의 다른 글
FileDownload in swift (0) | 2022.06.10 |
---|---|
Pod 설치 (0) | 2022.06.06 |