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

后端 未结 5 1023
时光说笑
时光说笑 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:47

    Others have already stated why its not working. Here is how you can cut down on the boilerplate and make your code more readable:

    @implementation NSManagedObject(MyPrivateAdditions)
    
    + (id)insertNewObjectInContext:(NSManagedObjectContext *)context
    {
        return [NSEntityDescription insertNewObjectForEntityForName:self.className inManagedObjectContext:context];
    }
    
    @end
    

    now you can do:

    Challenges *newChallenge = [Challenge insertNewObjectInContext:context];
    

提交回复
热议问题