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

前端之家收集整理的这篇文章主要介绍了ios – 由于未捕获的异常’NSInvalidArgumentException’而终止应用程序,原因:’entityForName:nil不是合法的NSManagedObjectContext前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我得到一个null结果

AppDelegate.h

@property (readonly,strong,nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly,nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly,nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

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

    }

我有一个xcdatamodeld文件,其实体为Person,属性为name.我从NSManagedObject创建了一个Person.h和.m文件.

为什么我的输出结果为null.

Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person''

解决方法

在你的viewController.m实现文件中,就在这段代码下面:
- (void)viewDidLoad
{

添加这段代码

id delegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [delegate managedObjectContext];
原文链接:https://www.f2er.com/iOS/330565.html

猜你在找的iOS相关文章