Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext

I am getting a null result for

AppDelegate.h

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, 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];
          .......

    }

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.

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''
like image 664
EK_AllDay Avatar asked May 05 '13 18:05

EK_AllDay


2 Answers

In your viewController.m implementation file, right under this bit of code:

- (void)viewDidLoad
{

add this bit of code:

id delegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [delegate managedObjectContext];
like image 119
Rawls Enterprises Avatar answered Oct 11 '22 21:10

Rawls Enterprises


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.

like image 29
EK_AllDay Avatar answered Oct 11 '22 22:10

EK_AllDay