NEW: Learn to build the incredible iOS 15 Weather app today! >>

Bookworm setup errors SwiftUI/CoreData

Forums > SwiftUI

Bookworm Project Tut 3/10 - using Xcode 11.6, MacOS 10.15.6 - two significant errors right out of the starting blocks:

Entity Student successfully created with 2 attributes, but:-

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

Use of undeclared type 'Student' error in ContentView.swift and

Type of expression is ambiguous without more context error in SceneDelegate.swift

SO & Google aren't very enlightning on these. Anybody have any idea, please.

Much obliged, Bruckner

2      

hi,

first, check the obvious: Codegen is set to Class Definition. this generates both the Student class definition file and a second file for its @NSManaged properties (and accessors, in some cases). if the option is set to Category/Extension, then you are responsible for creating the class definition for Student.

but, you probably already checked this.

even with the right choice, though, i have had this same thing happen multiple times: Core Data is working fine; i make a slight tweak in the model, perhaps just adding a new Entity; XCode goes nuts. i've also seen plenty of chatter about this on the Apple Developer's Forum.

the solution seems to be (and this has worked for me).

  • choose Product --> (hold down the option key) --> choose Clean Build Folder.
  • quit XCode
  • reopen XCode
  • build and hope that causes XCode to properly regenerate all the class files for Student and the other entities defined in the model.

this is not a bullet-proof procedure, by any means.

hope that helps,

DMG

2      

EDIT : it does bug me a bit so I posted this on stackoverflow too!! :) It seems that it is compiling at least when I restart Xcode, but still nothing on the simulator! https://stackoverflow.com/q/63603754/9497800

I have the same problem on Xcode 12 beta 5. Started the BookWorm tutorial, added 'Student' as entity in Core Data as in Video. Then when I do

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

I get the error "Student not found in scope.." I reset the project as suggested by @delawaremathguy and I get another error. This time the build succeeds but crashes at runtime with "Thread 1: "executeFetchRequest:error: A fetch request must have an entity."

The fun part is that if I start a new Swift 2.0 project and select "use Core Data" from the start, Apple has a start template which should show a list of Items and this fails in the same way.. I get the message "Item not found in scope" . Did someone noticed that? The apple default template with swiftUI and core data not compiling!

Maybe I am missing the obvious or maybe is a bigger bug? Anyway it seems that I am stuck now.

1      

Thank you @delawaremathguy

   

@multitudes - I also ran into the same issue. It seems that there are some additional configurations for Core Data with Xcode12 and iOS 14.

I checked out Persistence.swift and tweaked preview, replacing Item with Student. Here's the snippet:

struct PersistenceController {
    static let shared = PersistenceController()

    static var preview: PersistenceController = {
        let result = PersistenceController(inMemory: true)
        let viewContext = result.container.viewContext
        for _ in 0..<10 {
            let newStudent = Student(context: viewContext)
            newStudent.id = UUID()
        }

I am sure that I have more to change, but hopefully this is a step in the right direction.

2      

I solved this annoying issue of there being no entity and/or my persistant container not starting up soon enough by going into my coredata model and selecting the entity and inside the Inspector under Class, changed Module from whatever it was to 'Current Production Module'. It got my project to build, so I'm stoked.

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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.