I'm using this documentation as my base https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation
I'm failing to understand how I would simply adapt the app to load the json remotely
import Foundation
var landmarks: [Landmark] = load("landmarkData.json")
func load<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main
snippet from Model data.swift
the only example I've found is included below, however it's using an entirely different setup and does not use a detail view.
import SwiftUI
import Combine
struct ContentView: View {
@ObservedObject var fetcher = MovieFetcher()
var body: some View {
VStack {
List(fetcher.movies) { movie in
VStack (alignment: .leading) {
Text(movie.name)
Image(movie.thumbnail)
Text(movie.released)
.font(.system(size: 11))
.foregroundColor(Color.gray)
}
}
}
}
}
public class MovieFetcher: ObservableObject {
@Published var movies = [Movie]()
init(){
load()
}
func load() {
let url = URL(string: "https://wolvideos.web.app/videos.js")!
URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode([Movie].self, from: d)
DispatchQueue.main.async {
self.movies = decodedLists
}
}else {
print("No Data")
}
} catch {
print ("Error")
}
}.resume()
}
}
struct Movie: Codable, Identifiable {
public var id: Int
public var name: String
public var thumbnail: String
public var released: String
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case thumbnail = "thumbnail"
case released = "description"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
If possible can someone explain how and the best way to load a json file and then let the app format it into detail views?