The code below is my view that I am messing around with core data in but it keeps giving me the error that it cannot find the entity in the scope, yet the application runs fine and everything gets saved and fetched just fine.
Here are screenshots of the errors it gives
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(
entity: TestModelCoreData.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \TestModelCoreData.name, ascending: false)
]
) var entities: FetchedResults<TestModelCoreData>
var body: some View {
VStack {
Text("Hello, world!").padding()
Button(action: {
let newEntry = TestModelCoreData(context: self.moc)
newEntry.name = "New name"
if self.moc.hasChanges {
try? self.moc.save()
}
}) {
Text("Add entry")
}
List(entities, id: \.self) { entity in
Text(entity.name ?? "Unknown")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
This issue persists as of Xcode 12.2. It is not unique to CoreData. It can be triggered by e.g. creating an extension, then moving that extension to a separate file. If you code builds and runs despite a Swift compiler error "Cannot find 'xyz' in scope", try just closing and reopening your project before clearing caches, deleting derived data, etc.