[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext
I am getting a null result for 我得到一个null结果
AppDelegate.h AppDelegate.h
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
AppDelegate.m AppDelegate.m
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Person *newPerson = [NSEntityDescription
insertNewObjectForEntityForName:@"Person"
inManagedObjectContext:self.managedObjectContext];
.......
}
I have one xcdatamodeld file with a entity of Person and a attribute of name. 我有一个xcdatamodeld文件,其实体为Person,属性为name。 I made a Person.h and .m file from a NSManagedObject. 我从NSManagedObject创建了一个Person.h和.m文件。
Why am I getting a null result for my output. 为什么我的输出结果为null。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person''
In your viewController.m implementation file, right under this bit of code: 在你的viewController.m实现文件中,就在这段代码下面:
- (void)viewDidLoad
{
add this bit of code: 添加这段代码:
id delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = [delegate managedObjectContext];
I realized that since I was not adding Core Data when I originally created the project, I was missing some code that is automatically generated when the core data option is ticked. 我意识到,由于我最初创建项目时没有添加Core Data,因此我错过了一些在核心数据选项被选中时自动生成的代码。 Thus, when I followed some tutorials online, this automatically generated code is assumed. 因此,当我在线学习一些教程时,会假设这个自动生成的代码。 That's where I messed up. 这就是我搞砸了的地方。
确保在引发异常时运行的代码正在您创建托管对象上下文的同一线程上运行。
如果您使用的是使用NSPersistentContainer的项目模板,请确保在iOS 10+上运行此模板,否则当您执行某种Core Data操作时,它会因初始化而崩溃并崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.