我正在尝试在我已经开始的项目中设置coredata ..我已经解决了其他一些错误,但现在已经缩小到了这个,
***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'+ entityForName:无法 找到实体名称'Manuf''
的NSManagedObjectModel
我想我已经缩小了这个问题,并且可以解决这个问题并回答here:Alex的回答。
然而,我并不完全确定这是我的情况,因为我混淆的原因是,而不是在我的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;
}
因为我在对象类中做了所有的事情,我不知道如何在我的app delegate中初始化它?
与我现在在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;
}
答案 0 :(得分:2)
上午。
我要检查三件事:
1)self.managedObjectContext
nil
? (如果你NSLog(@"%@", self.managedObjectContext);
,你会得到什么?
2)你的模型描述是否包含一个名为Manuf
的实体 - 它可能区分大小写:)
3)创建协调器时,是否正确加载(2)中的模型?