简体   繁体   English

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ entityForName:nil不是合法的NSManagedObjectContext

[英]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.

相关问题 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“数据参数为nil” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' iOS - CoreData - TableViewController - NSInvalidArgumentException',原因:'+ entityForName:nil不是合法的NSManagedObjectContext - iOS - CoreData - TableViewController - NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext **由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'应用程序试图在目标上显示nil模态视图控制器 - ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:键不能为零” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView必须使用非nil布局参数初始化 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter Swift:由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“实体名称不能为零。” - Swift : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Entity name must not be nil.' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在目标上显示nil模态视图控制器 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 得到“ ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFNumber compare:]:nil参数'” - getting “*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'” 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:对象不能为nil(键:索引)” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: index)' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ ***-[__ NSCFConstantString stringByAppendingString:]:无参数” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM