Search code examples
swiftuicore-data

SwiftUI View - initializer is inaccessible due to 'private' protection level


I have List in one view and upon selection of a row, would like to navigate to another View. I am trying to pass 'LocationInfra', a coredata entity to another ConfigProfileView. I get two errors in the SummaryView, when I call ConfigProfileView via NavigationLink

'ConfigProfileView' initializer is inaccessible due to 'private' protection level Cannot convert value of type 'FetchedResults.Element' (aka 'LocationInfra') to expected argument type 'Binding<LocationInfra?>'

struct summaryView: View {
 @State public var selectedLocationInfra: LocationInfra? = nil
   var body: some View {
    NavigationView {
        ScrollView(showsIndicators: false){
             ForEach(locationProfiles) { locationProfile in
                 NavigationLink (""){
                   ConfigProfileView(locationInfra: selectedLocationInfra) // get two errors here
                 }
                 label: do {
                   HStack {
                     // some code here
                   }.onTapGesture {
                            selectedLocationInfra = locationProfile
                }

             }
        }
    }
     .navigationTitle("Summary")
  }

}

view to be displayed

 struct ConfigProfileView: View {
   @Environment(\.managedObjectContext) private var viewContext
   @Binding private var locationInfra: LocationInfra? 

    NavigationView {
        ScrollView(showsIndicators: false) {
            VStack (alignment: .leading ) {
                 Text(locationInfra?.locationName!)
            }
        }
    }
  }

How to fix the errors?


Solution

  • I'm assuming your array could be like this:

    struct SummaryView: View {
        var locationProfiles: [LocationInfra] = [
            .init(index: 0, locationName: "US"),
            .init(index: 1, locationName: "UK"),
            .init(index: 2, locationName: "SG")
        ]
        
        var body: some View {
            NavigationView {
                ScrollView(showsIndicators: false) {
                    ForEach(locationProfiles) { locationProfile in
                        NavigationLink {
                            ConfigProfileView(locationInfra: locationProfile)
                        } label: {
                            Text("Index: \(locationProfile.index)")
                        }
                    }
                }
                .navigationTitle("Summary")
            }
        }
    }
    

    Then in ConfigProfileView, just for displaying:

    struct ConfigProfileView: View {
        @Environment(\.managedObjectContext) private var viewContext
        var locationInfra: LocationInfra?
        
        var body: some View {
            NavigationView {
                ScrollView(showsIndicators: false) {
                    VStack (alignment: .leading ) {
                        Text(locationInfra?.locationName ?? "unknown")
                    }
                }
            }
        }
    }
    

    Output:

    enter image description here