TimeCard[41935:10278070] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'TimeCard.CountdownItem' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'TimeCard.CountdownItem' so +entity is confused. Have yo
u loaded your NSManagedObjectModel yet ?
TimeCard[41935:10278070] [error] error: +[TimeCard.CountdownItem entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
Failed to find a unique match for an NSEntityDescription to a managed object subclass
executeFetchRequest:error: A fetch request must have an entity.
以上是报错信息。
最后定位到问题ContentView里的@Environment不生效,获取不到环境变量managedObjectContext
将
@Environment(\.managedObjectContext) var managedObjectContext
@Environment(\.managedObjectContext) var managedObjectContext
改为
let managedObjectContext: NSManagedObjectContext = ((NSApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext)!
就好了(因为开发的macos app,所以用的是NSApplication)