1

没什么好说的,我已经为核心数据做了另一个应用程序,一切都很顺利。

这个应用程序给出了一个奇怪的错误。

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

*** First throw call stack:(0x1faa012 0x13e7e7e 0x10edf57 0x1134052 0x3211 0x11a04b 0x2fe2 0x22c1 0x19157 0x19747 0x1a94b 0x2bcb5 0x2cbeb 0x1e698 0x1f05df9 0x1f05ad0 0x1f1fbf5 0x1f1f962 0x1f50bb6 0x1f4ff44 0x1f4fe1b 0x1a17a 0x1bffc 0x20dd 0x2005) 
vlibc++abi.dylib: terminate called throwing an exception

我尝试仅使用应用程序委托和 mainViewController 来简化我的应用程序,在 MainViewController 中我尝试向我的 Coredata 添加一个 pbject 只是为了查看是否一切正常。这是我的模型

应用委托

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  MasterViewController *masterViewController = [[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil];
  masterViewController.managedObjectContext = self.managedObjectContext;
  [self.window setRootViewController:masterViewController];

  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];
  return YES;
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
    return _managedObjectContext;
}

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

- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
    return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"pizzaCoreData" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}


-  (NSPersistentStoreCoordinator *)persistentStoreCoordinator
 {
 if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory]  URLByAppendingPathComponent:@"pizzaCoreData.sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

     Typical reasons for an error here include:
     * The persistent store is not accessible;
     * The schema for the persistent store is incompatible with current managed object model.
     Check the error message to determine what the actual problem was.


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

     If you encounter schema incompatibility errors during development, you can reduce their frequency by:
     * Simply deleting the existing store:
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
     @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return _persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

主视图控制器

- (void)viewDidLoad
{
 [super viewDidLoad];
 TableIngredientsViewController *tableIngredientVC = [[TableIngredientsViewController alloc]init];


 TablePizzasViewController *tablePizzaVC = [[TablePizzasViewController alloc]init];

 NSManagedObject *newPizza = [NSEntityDescription insertNewObjectForEntityForName:@"Ingredient" inManagedObjectContext:_managedObjectContext];

 [newPizza setValue:@"TIMIDA" forKey:@"name"];

 NSError *error;

 if (![_managedObjectContext save:&error]){
    NSLog(@"Error %@ %@", error, [error userInfo]);
 }

 UINavigationController *ingredientNavController = [[UINavigationController alloc]initWithRootViewController:tableIngredientVC];
 UINavigationController *pizzaNavController = [[UINavigationController alloc]initWithRootViewController:tablePizzaVC];
 [self setViewControllers:@[pizzaNavController, ingredientNavController]];
}

这是模型的截图: http ://tinypic.com/view.php?pic=97nrcn&s=5

我已经更新了我的 AppDelegate,我忘记了复制最后一个方法

4

2 回答 2

0

您必须从您的 appDelegate 中获取 managedObjectContext。所以在你的 MainController 而不是:

_managedObjectContext

利用:

((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext
于 2013-09-04T18:00:37.393 回答
0

执行此操作时,您的 _managedObjectContext 为零

NSManagedObject *newPizza = [NSEntityDescription insertNewObjectForEntityForName:@"Ingredient" inManagedObjectContext:_managedObjectContext];

因此,在将上下文传递给视图控制器之前,请确保它已正确初始化。

于 2013-09-04T17:43:21.667 回答