swiftuiswiftui-table

swiftui table view, No exact matches in call to initializer


I'm trying to set up selection in a swiftui table so that I can use the selection to navigate to a form view.

If I add selection to the table, I get the 'No exact matches in call to initializer' error.

I have tried troubleshooting but haven't got very far.

import SwiftUI
import CoreData

struct CaseloadView: View {
    @FetchRequest(
        entity: StudentRecordEntity.entity(),
        sortDescriptors: [
            NSSortDescriptor(keyPath: \StudentRecordEntity.schoolClassCode, ascending: true),
            NSSortDescriptor(keyPath: \StudentRecordEntity.lastName, ascending: true)
        ]
    ) var studentRecords: FetchedResults<StudentRecordEntity>
    
    @State private var selectedStudentID: NSManagedObjectID? = nil
    
    var body: some View {
        NavigationView {
            Table(studentRecords, selection: $selectedStudentID) {
                TableColumn("First Name", value: \.firstName)
                TableColumn("Last Name", value: \.lastName)
                TableColumn("DOB") { record in
                    Text(DateFormatter.customDate.string(from: record.dob))
                }
                TableColumn("Class", value: \.schoolClassCode)
                TableColumn("Status", value: \.currentStatus)
                TableColumn("Consent Type", value: \.consentType)
                TableColumn("Consent Date") { record in
                    Text(DateFormatter.customDate.string(from: record.consentDate))
                }
            }
            .navigationTitle("Caseload")
            .toolbar {
                ToolbarItem(placement: .primaryAction) {
                    NavigationLink(
                        destination: Caseload_AddStudentView(
                            student: studentRecords.first { $0.objectID == selectedStudentID }
                        )
                    ) {
                        Text("Add/Edit")
                    }
                    .disabled(selectedStudentID == nil)
                }
            }
        }
    }
}

Solution

  • Make StudentRecordEntity conform to Identifiable

    extension StudentRecordEntit: Identifiable {
       var id: NSManagedObjectID {
           self.objectID
       }
    }