CoreData issue SwiftUI

78 views Asked by At

I am new in Swift and am working on a project using core data. I created my entity (called StudentData) comprehensive of all the attributes that I am using in my coding. I have a view with a list of clients and when you tap on each of them a view with some attributes will show, here is the code:

struct StudentView: View {
    @Environment (\.presentationMode) var presentationMode
    @Environment(\.managedObjectContext) var managedObjectContext
    @State private var showingSheet = false
    @State var selectedStudent = StudentData()
    let myStudent: StudentData
    
var body: some View {
            HStack {
                        Text("Subject")
                            .font(.system(size: 30, weight: .bold, design: .rounded))
                            .foregroundColor(.red)
                            .padding(.horizontal)
                         Spacer()
                       Text("\(myStudent.subject ?? "No Subject")")
                        .font(.title2)
                        .bold()
                        .foregroundColor(.white)
                        .padding(.horizontal)
                
            }
               HStack {
                Text("School Year")
                        .font(.system(size: 30, weight:.bold, design: .rounded))
                        .foregroundColor(.red)
                            .padding(.horizontal)
                    
                        Spacer()
                Text("Year \(myStudent.schoolYear ?? "Unknown")")
                    .font(.title2)
                    .bold()
                    .foregroundColor(.white)
                    .padding(.horizontal)
 }
            HStack {
               Text("Address")
                        .font(.system(size: 30, weight: .bold, design: .rounded))
                        .foregroundColor(.red)
                        .italic()
                            .padding(.horizontal)
                        Spacer()
                        Spacer()
                Button(action: {
                            showingSheet = true
                        }) {
                            Text(("\(myStudent.address ?? "Unknown")")).underline()
                        }
 }.navigationBarTitle(myStudent.name ?? "No Name")
         .navigationBarItems(trailing:
                                NavigationLink(destination: DetailView(myStudent:selectedStudent)) {
                                Text("More Details")

Moving to the DetailView the code is:

@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: StudentData.entity(), sortDescriptors: [],
animation: .spring()) var chapter: FetchedResults<StudentData>
@Environment (\.presentationMode) var presentationMode
@State var showModalView = false
let myStudent: StudentData
@State var selectedStudent = StudentData()
var body: some View {
VStack{
                        Text("Topics Covered")
                            .font(.system(size: 30, weight: .bold, design: .rounded))
                            .foregroundColor(.red)
                            .padding(.horizontal)
                    Text("\(myStudent.chapter ?? "-")")
                     .font(.title2)
                     .bold()
                     .foregroundColor(.white)
                     .padding(.horizontal)
                    }
  }.sheet(isPresented: $showModalView, content: {
            TopicView(topic: selectedStudent)})

However when I tap on the button to show the detail view the app crashes and this error shows: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[StudentData chapter]: unrecognized selector sent to instance 0x60000157b380' terminating with uncaught exception of type NSException. So I understand the error is clearly related to the attribute 'chapter' in my entity but I don't really figure out what I am doing wrong. Thanks for anyone who'll help.

0

There are 0 answers