insertNewObjectForEntityForName:

前端 未结 3 1658
小蘑菇
小蘑菇 2020-12-01 19:24

I set up an Entity using the Xcode .xcdatamodel file editor. I created an entity called Person, added a few attributes, then generated a .m file to represent it. That all

3条回答
  •  情书的邮戳
    2020-12-01 19:50

    The fact that you didn't set up the MOC is almost certainly the problem. Most specifically, it means you're probably not loading your MOM (Managed Object Model) that defines Person. Somewhere in your code you should have something like this:

    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    

    And something like this:

    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    

    And something like this:

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    

    I'm just copying lines out of the AppDelegate of the Core Data template (what you get if you make a new application that uses Core Data).

    If you have all that, make sure that your xcdatamodel is listed in your Compile Sources step of the build. And of course make sure that Person is actually the Entity name in your xcdatamodel. Entity name is not the same as Class, though they are often set to be the same.

提交回复
热议问题