NSManagedObject initWithEntity: insertIntoManagedObjectContext: EXC_BAD_ACCESS

I have a base data store configured using a subclass of NSManagedObject: Note.

I can use NSEntityDescription insertNewObjectForEntityName: inManagedObjectContext: no problem, 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 see that NSManagedObjectContext * really points to a valid object.

Any help would be appreciated!

+4
source share
1 answer

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

 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:moc]; Note* note = [[Note alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:moc]; 
+14
source

Source: https://habr.com/ru/post/1343087/


All Articles