CoreData 报错 Failed to call designated initializer on NSManagedObject class
The problem lies not in the code in your question, but in the snippet you included as comments to the other answer:
1 | |
This doesn’t just declare currentCourse to be of type Course, it also creates an instance of the Course entity using the standard init method. This is expressly not allowed: You must use the designated initialiser: init(entity entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?). This is described in the Apple Documentation here.
I suspect you do not ever use the instance created by the above var definition, so just define it as being of type Course?:
1 | |
Since it is optional, you do not need to set an initial value, though you will need to unwrap the value whenever it is used.
CoreData 报错 Failed to call designated initializer on NSManagedObject class
https://wonderhoi.com/2023/10/10/CoreData-报错-Failed-to-call-designated-initializer-on-NSManagedObject-class/