Swift can't find the Entity Core Data

I'm learning Swift and I'm finding some issues to learn it also because my background is as .NET developer and I'm thinking in a different way.

So, I'm following a book and I'm trying to add a Core Data to my project. You have the full source code on GitHub. In Xcode a pass a new Entity for Core Data called ToDoCR, very basic.

When I try to use the ToDoCR entity, I get some errors from Xcode.

Cannot find type 'ToDoCD' in scope


override func viewDidLoad() {
    super.viewDidLoad()
}

func getToDos() {
    if let context = (UIApplication.shared.delegate as? 
       AppDelegate)?.persistentContainer.viewContext {
        if let toDosFromCoreData = try? context.fetch(ToDoCD.fetchRequest()) {
            if let toDos = toDosFromCoreData as? [ToDoCD] {
                todoCDs = toDos
                tableView.reloadData()
            }
        }
    }
}

Also, when I use the instance of the ToDo Core Data and I perform a boxing, I get another kind of errors.

Type of expression is ambiguous without more context

Do I have to import a library to access the Core Data? What did I miss in this code?

I'm not really fluent in CoreData, but I think you should declare a struct:

struct ToDoCD {
  var name: String
  var priority: Int32
}

I have downloaded your GitHub project and tried to build it, and showed Succeeded.

Some classes (not structs) representing Core Data entity are generated dynamically. So, some errors might be shown until the classes are acknowledged by the editing support.

You can try Building the project (Cmd+B) even when some errors are still shown.

I was wrong. As OOPer explained, If you option-click on ToDoCD, you should be sent to ToDoCD class definition.

//
//  ToDoCD+CoreDataClass.swift
//  ToDoCD
//
//  Created by Claude31 on 22/08/2021.
//
//  This file was automatically generated and should not be edited.
//

import Foundation
import CoreData

@objc(ToDoCD)
public class ToDoCD: NSManagedObject {

}
Accepted Answer

I tried to clean the build and rebuild the project a lot of times with the same result. Then, I was tired and I closed Xcode.

Basically, if I close Xcode and reopen the project, the project seems find and Xcode can compile it without showing errors and also I can see the ToDo properties.

I hope I don't have to do the same every time I change the Core Data.

Thank you everybody for the replies.

Swift can't find the Entity Core Data
 
 
Q