Swift學習筆記:No NSEntityDescriptions in any model claim the NSManagedObject subclass 解決方法

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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章