UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Why does my DetailView not update after first selection

Forums > SwiftUI

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.

2      

Add "List" to enclose ForEach

    var body: some View {
        NavigationView {
          List {
            ForEach(items, id: \.self) { item in
                NavigationLink("\(item.name)", destination: DetailView(data: item))
                      .padding(.trailing)
              }
              VStack {
                  Text("Select One")
              }
            }
        }.navigationTitle("Services Parser")
    }

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.