Swift – Xcode error saying “cannot find type ‘TestModelCoreData’ in scope when using core data fetch request, yet it compiles and runs


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
        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 {
Related Question