我正在尝试使用Core Data为macOS运行基本的测试SwiftUI应用程序,但遇到了问题。当我在视图中使用它时:
@FetchRequest(entity: Note.entity(),sortDescriptors: []) let notes: FetchedResults<Note>
该应用程序崩溃并出现以下错误:
Notetaker [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'Notetaker.Note' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'Notetaker.Note' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
Notetaker [error] error: +[Notetaker.Note entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[Notetaker.Note entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
Notetaker executeFetchRequest:error: A fetch request must have an entity.
现在,如果我使用FetchRequest的替代形式,则尽管代码丑陋得多,但它仍然可以正常工作:
// outside of the view
func getallNotes() -> NSFetchRequest<Note> {
let request: NSFetchRequest<Note> = Note.fetchRequest()
request.sortDescriptors = []
return request
}
// in the view
@FetchRequest(fetchRequest: getallNotes()) var notes: FetchedResults<Note>
此外,如果我将其转为iOS应用,则@FetchRequest的实体版本可以正常工作。
有什么想法吗?