Search code examples
swiftlistswiftuinavigationview

How debug my code in List with NavigationView


On the Canvas my code start correctly, but simulator send message about bug

import SwiftUI

struct ContentView: View {
    //1
    var persons: [Member] = []
    
    var body: some View {
        NavigationView() {

break point stopped here

            List(persons) { person in
                NavigationLink(destination: {
                    MemberDetail(name: person.name, headline: person.headline, bio: person.bio)
                }, label: {
                    HStack {
                        Image(person.imageName).cornerRadius(40.0)
                        
                        VStack(alignment: .leading) {
                            Text(person.name)
                            Text(person.headline)
                                .font(.subheadline)
                                .foregroundColor(.gray)
                        }
                    }
                })
            }
            .navigationTitle(Text("Persons"))
        }
    }
}

expectation

enter image description here

reality enter image description here

Code struct of MemberDetails

struct MemberDetail: View {
    var name: String
    var headline: String
    var bio: String
    
    var body: some View {
        VStack {
            Image(name)
                .clipShape(Circle())
                .overlay(Circle().stroke(Color.orange, lineWidth: 4))
                .shadow(radius: 10)
            Text(name)
                .font(.title)
            Text(headline)
                .font(.subheadline)
            Divider()
            Text(bio)
                .font(.headline)
                .multilineTextAlignment(.center)
                .lineLimit(50)
        }
            .padding(20)
            .navigationBarTitle(Text(name), displayMode: .inline)
    }
}

break points get error. I run my project on iphone 7 and simulator, but this not give solution


Solution

  • I completely rewrote the code according to the guides from apple and it all worked

    struct LandmarkList: View {
        var body: some View {
            NavigationView {
                List(landmarks) { landmark in
                    NavigationLink {
                        LandmarkDetail(landmark: landmark)
                    } label: {
                        LandmarkRow(landmark: landmark)
                    }
                }
                .navigationTitle("Landmarks")
            }
        }