CoreData: error: Failed to call designated initializer on NSManagedObject class

后端 未结 5 1030
时光说笑
时光说笑 2020-12-01 10:08

I have a little damn problem with CoreData. I want to insert a new Object, so I first have to create one. This is done by that code:

Challenges *newChallenge         


        
5条回答
  •  甜味超标
    2020-12-01 10:55

    NSManagedObject cannot be just alloc/init like you would normally do with an NSObject. As a matter of fact the designated initializer is: initWithEntity:insertIntoManagedObjectContext:

    Now, for the actual error, Apple states in the documentation that:

    Important: This method is the designated initializer for NSManagedObject. You must not initialize a managed object simply by sending it init.

    So, you could see that you need 2 things in order to initialize it, an NSEntityDescription (which entity you intend to instantiate) and an NSManagedObjectContext (the context that the new object will be created into).

提交回复
热议问题