xcode 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+entityForName:nil 不是合法的 NSManagedObjectContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16387935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext
提问by EK_AllDay
I am getting a null result for
我得到一个空结果
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. I made a Person.h and .m file from a NSManagedObject.
我有一个带有 Person 实体和 name 属性的 xcdatamodeld 文件。我从 NSManagedObject 制作了一个 Person.h 和 .m 文件。
Why am I getting a null result for my output.
为什么我的输出结果为空。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person''
采纳答案by EK_AllDay
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. Thus, when I followed some tutorials online, this automatically generated code is assumed. That's where I messed up.
我意识到,由于我在最初创建项目时没有添加核心数据,所以我缺少一些在核心数据选项被勾选时自动生成的代码。因此,当我在网上学习一些教程时,会假设这个自动生成的代码。那就是我搞砸的地方。
回答by Rawls Enterprises
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];
回答by bdev
Make sure that the code that is running when the exception is raised is being run on the same thread that you created the managed object context on.
确保在引发异常时运行的代码在创建托管对象上下文的同一线程上运行。
回答by Raimundas Sakalauskas
If you are using project template that uses NSPersistentContainer, make sure you run this on iOS 10+ or it will fail silently with initialization and crash when you perform some sort of Core Data operation.
如果您使用的是使用 NSPersistentContainer 的项目模板,请确保在 iOS 10+ 上运行它,否则它会在初始化时静默失败并在您执行某种核心数据操作时崩溃。