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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.