【发布时间】:2015-04-06 15:48:50
【问题描述】:
我是 Objective C、iOS 和 CoreData 的新手,我目前正在尝试将一些数据保存到我在 CoreData 中的实体。
当尝试查找带有insertNewObjectForEntityForName 的实体时,它似乎正在返回nil,基于以下错误。
错误:
2015-04-06 17:46:12.274 Bugland[18623:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Scores''
LeaderboardViewController.m
JPAppDelegate *JPAppDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =[JPAppDelegate managedObjectContext];
NSManagedObject *newScore;
newScore = [NSEntityDescription
insertNewObjectForEntityForName:@"Scores"
inManagedObjectContext:context];
[newScore setValue: scoreToAddAsString forKey:@"score"];
NSError *error;
[context save:&error];
【问题讨论】:
-
调试,检查
context是否存在。崩溃前记录的任何错误? -
检查值和/或上下文是否存在的最佳方法是什么?只需将其记录到控制台还是?
-
可以,或者加个断点和
po -
好的,如果我在下面定义
*context的地方注释掉所有内容,我不会崩溃,如果我尝试记录上下文,控制台将一无所获。嗯 -
因此您需要查看 + 显示您的
managedObjectContext方法实现,并查看您的核心数据堆栈是否正在设置中
标签: ios objective-c core-data