swiftUI - how to pass core data values of an item from ForEach list in NavigationLink to Detail view

I just cannot figure out how to pass core data values of an item from ForEach list in NavigationLink to Detail view. Here is the code that got error: "Cannot convert value of type 'FetchedResults.Element' (aka 'FileEnt') to expected argument type 'FileViewModel'"

struct FileList: View {

    @Environment(\.managedObjectContext) var context
    @FetchRequest(entity: FileEnt.entity(), sortDescriptors: [NSSortDescriptor(key: "fileName", ascending: false)]) var results: FetchedResults<FileEnt>
    @ObservedObject var fileData : FileViewModel

    var body: some View {
        NavigationView {
            List {
                ForEach(results) { aFile in
                    NavigationLink(destination: FileDetails(fileData: aFile), label: {
                    // ** error on red-underscored aFile above.
                            Text(aFile.fileName ?? "")
                    })
                }
            }.navigationBarTitle("Files")
        }
    }
}

FileViewModel is for Add, Edit and Detail views. Here is its simplified version for the question:

class FileViewModel: ObservableObject {
    @Published var fileName = ""
    
    init(){

    }

    func DetailItem(fileItem: FileEnt){
        fileName = fileItem.fileName ?? ""
    }
}

Modified question for easy understanding. Thanks for any advice.



from Recent Questions - Stack Overflow https://ift.tt/3nrxcC3
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation