“无法加载NSManagedObjectModel。 nil是非法的URL参数“ [英] "Cannot load NSManagedObjectModel. nil is an illegal URL parameter"
问题描述
我想从AppDelegate获取托管对象上下文,但应用程序崩溃后,我把两行代码进入方法,即使我没有别的,并在调试区域有一个消息:CoreData:无法加载NSManagedObjectModel。nil是一个非法的网址参数...
I want to get the managed object context from AppDelegate, but the app crashed after I put the two lines of code into the method even I did nothing else, and there was a message in debug area:"CoreData: Cannot load NSManagedObjectModel. nil is an illegal URL parameter..."
在我的方法中添加的代码:
The code added in my method:
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *managedObjectContext = delegate.managedObjectContext;
AppDelegate中的-managedObjectModel方法:
-managedObjectModel method in AppDelegate:
- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FoodPin" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
和-managedObjectContext方法:
and the -managedObjectContext method:
- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
FoodPin是我的专案名称这里有什么问题?我是iPhone新手(特别是Core Data)。
"FoodPin" is my project name.So what's wrong here?I'm new to iPhone programming (Core Data in particular).
任何人都可以帮我吗?
感谢...
推荐答案
问题是这一行:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FoodPin" withExtension:@"momd"];
modelURL
是 nil
意味着系统找不到资源 FoodPin.momd
。
modelURL
is nil
meaning that the system couldn't find the resource FoodPin.momd
.
确保您的项目中有一个Core Data模型名为 FoodPin
。它将在项目导航器中显示为 FoodPin.xcdatamodeld
。
Make sure you have a Core Data model in your project named FoodPin
. It will appear as FoodPin.xcdatamodeld
in the Project Navigator.
这篇关于“无法加载NSManagedObjectModel。 nil是非法的URL参数“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!