UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 53: Bookworm - Use of undeclared type 'Student' error when using Core Data

Forums > 100 Days of SwiftUI

I'm having an issue with adding this code to ContentView as described in the lesson:

@FetchRequest(entity: Student.entity(), sortDescriptors: []) var students: FetchedResults<Student>

It is giving the error "Use of undeclared type Student"

After searching the internet some, I found a post that suggested:

  1. right-clicking the Bookworm.xcdatamodeld file
  2. select 'Show in Finder'
  3. drag the .xcdatamodel file into your project (Note: the lack of the letter 'd' on the end of the file extension)
  4. delete the old xcdatamodeld file from your project

This appears to have work, as the error went away. But why did this work? Why couldn't Xcode recognize the Student entity that I created in my xcdatamodeld file?

Has anybody else experienced this? I'd like to understand better what happened.

3      

i have had this issue before its due to Xcode not being able to find the automatically generated class. in your case Student Class is automatically generated & Xcode can detect that. The solutions i use is:

  • Clean Build Folder : ⇧ + ⌘ + K
  • closed the Xcode and reopen it
  • Build project : ⌘ + B

12      

Thanks for the response! I spent half the day yesterday googling/trying different methods until the one that you suggested finally did the trick.

3      

Thanks byaruhaf, that tricked solved it for me too!

3      

This did not work for me.. im still getting the same errors

3      

Thanks! Worked for me.

3      

For me, this fixed the issue but created another: ! Cannot preview in this file -- Bookworm.app may have crashed. Check...

3      

Unfortunately, I am experiencing the same problem as @smithi01, after following the steps suggested by @byaruhaf. I've been able to narrow the problem down somewhat - XCode compiles and runs the code just fine until i add the list code:

List {
    ForEach(students, id: \.id) { student in
        Text(student.name ?? "Unknown")
    }
}

I've tried copying and pasting the code, typing out the code, closing and re-opening XCode, restarting the computer - nothing works. XCode only displays the "Cannot preview this file - Bookworm.app may have crashed" error message. XCode 11.6, Mac OS Catalina 10.15.5.

UPDATE for anyone else having this problem. I was previewing the app in XCode's canvas. Can't remember where i came across this, but I saw somewhere that the canvas preview and Core Data don't always get along - something to do with errors when attempting to load the data. At any rate, I switched to using the simulator (as Paul was showing in his video) and it worked.

3      

Thank you byaruhaf - it works!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.