I am looking to understand how to process both XML and JSON versions of a file in SwiftUI. I created a simple NavigationVIew that lists three files and should provide some details on each file based on selecting the item in the NavigationView. However, after I select the first entry (regardless which one I pick) the Detail view never changes: Here's the code:
struct ContentView: View {
var items = [Item]()
init() {
items.append(Item.init(name: "Root Services", type: "xml", data: readLocalFile(forName: "RootServicesRDF", ofType: "xml")!))
items.append(Item.init(name: "Root Services JSON", type: "json", data: readLocalFile(forName: "RootServicesJSON", ofType: "json")!))
items.append(Item.init(name: "Resource RDF", type: "xml", data: readLocalFile(forName: "ResourceRDF", ofType: "xml")!))
}
var body: some View {
NavigationView {
ForEach(items, id: \.self) { item in
NavigationLink("\(item.name)", destination: DetailView(data: item))
.padding(.trailing)
}
VStack {
Text("Select One")
}
}.navigationTitle("Services Parser")
}
private func readLocalFile(forName name: String, ofType: String) -> Data? {
do {
if let bundlePath = Bundle.main.path(forResource: name,
ofType: ofType),
let data = try String(contentsOfFile: bundlePath).data(using: .utf8) {
return data
}
} catch {
print(error)
}
print("Got here without a valid return but no error, Path = \(String(describing: Bundle.main.path(forResource: name, ofType: ofType))) for \(name) of type \(ofType)")
return nil
}
}
The Detail view currently just shows the name and type from the items array.