Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSManagedObject initWithEntity: insertIntoManagedObjectContext: EXC_BAD_ACCESS

I have a Core Data store set up with an Xcode-generated subclass of NSManagedObject: Note.

I can use NSEntityDescription insertNewObjectForEntityName: inManagedObjectContext: with no trouble, but when I try to do this:

    NSManagedObjectContext* moc = [(QuickTextAppDelegate*)([[UIApplication sharedApplication] delegate]) managedObjectContext];

    Note* note = [[Note alloc] initWithEntity:@"Note" insertIntoManagedObjectContext:moc];

I get an EXC_BAD_ACCESS error.

Using breakpoints I can see that the NSManagedObjectContext* in indeed pointing to a valid object.

Any help would be appreciated!

like image 442
Liam Hughes Avatar asked Mar 09 '11 23:03

Liam Hughes


1 Answers

I see at least one problem there: initWithEntity:insertIntoManagedObjectContext: takes an NSEntityDescription, not an NSString. Try something like this:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:moc];
Note* note = [[Note alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:moc];
like image 92
Anomie Avatar answered Sep 29 '22 19:09

Anomie