Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 = [[Challenges alloc] init];
[newChallenge setName:@"TestChallenge"];
[newChallenge setRounds:[[NSNumber alloc] initWithInt:12]];
[newChallenge setShots:[[NSNumber alloc] initWithInt:5]];
[newChallenge setDate:[NSDate date]];

But however after the alloc init I get this error:

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

What the hack is going wrong?

like image 231
Bene Avatar asked Feb 03 '13 10:02

Bene


4 Answers

I think the problem is that Challenges is a NSManagedObject class and you need the designated initializer:

initWithEntity:insertIntoManagedObjectContext:

instead of

Challenges *newChallenge = [[Challenges alloc] init];

Read More..

like image 58
Leo Chapiro Avatar answered Nov 10 '22 14:11

Leo Chapiro


In case you ask yourself "OK, so how to I get that entity?" (like I did), you do this by using the entityForName method like so:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Challenges" inManagedObjectContext:self.managedObjectContext];

Challenges *newChallenge = [[Challenge alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];

Hope this helps, this thread has helped me a lot!

like image 39
Jay Versluis Avatar answered Nov 10 '22 14:11

Jay Versluis


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).

like image 30
Alladinian Avatar answered Nov 10 '22 15:11

Alladinian


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];
like image 4
bernstein Avatar answered Nov 10 '22 14:11

bernstein