NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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.

   

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")
    }

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.