开发者

NSManagedObject initWithEntity: insertIntoManagedObjectContext: EXC_BAD_ACCESS

开发者 https://www.devze.com 2023-02-16 01:48 出处:网络
I have a Core Data store set up with an Xcode-generated subclass of NSManagedObject: Note. I can use NSEntityDescription insertNewObj开发者_JS百科ectForEntityName: inManagedObjectContext: with no tro

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

I can use NSEntityDescription insertNewObj开发者_JS百科ectForEntityName: 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!


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号