0

我正在尝试在我已经开始的项目中设置 coredata.. 我已经解决了一些其他错误,但现在已将其范围缩小到此,

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“Manuf”的 NSManagedObjectModel

我想我已经缩小了这个问题的范围,并在这里回答了这个问题的可能解决方案:亚历克斯的回答。

但是,我并不完全确定这对我来说是不是这样,因为我感到困惑的原因是,我实际上并没有在我的 app-delegate 和 viewcontrollers 中设置所有内容,而是使用了我的 app-delegate 和一个对象类。所以我希望有人可以帮助我在这里隔离并解决我的问题......

这是我的对象类中的代码段,这给了我这个问题。它几乎与 xcode 为 coredata 应用程序生成的模板代码相同,但是它不包括排序和 tableview 的东西,因为我不需要。我只是将一堆 NSData 转储到我的 coredata 对象中。

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Manuf" inManagedObjectContext:self.managedObjectContext]; //this is the line where my code fails and generates the error in the log
    [fetchRequest setEntity:entity];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&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. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
} 

更新:

这就是我认为我出错的地方。在模板代码中,控制器和上下文在 appdelegate 中设置,如下所示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    ICDMasterViewController *controller = (ICDMasterViewController *)navigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;
    return YES;
}

因为我在一个对象类中做我所有的事情我不知道如何在我的应用程序委托中初始化它?

与我现在在我的 appdelegate 中尝试做的相反

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    //Add status bar
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

    self.window.rootViewController = self.navigationController; //Adds RootViewController to the NavigationController interface
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];

    //try setting up context for my nsobjectclass EngineResponses
    EngineResponses *engineResponses = [[EngineResponses alloc] init];
    engineResponses.managedObjectContext = self.managedObjectContext;

    return YES;
}
4

1 回答 1

2

早晨。

我要检查三件事:

1) 是self.managedObjectContext nil吗?(如果你得到什么NSLog(@"%@", self.managedObjectContext);

2)您的模型描述是否包含一个名为的实体Manuf- 它可能区分大小写:)

3)当您创建协调器时,您是否从 (2) 正确加载模型?

于 2012-04-29T22:29:31.100 回答