“Cannot load NSManagedObjectModel. nil is an illegal URL parameter”

放肆的年华 提交于 2020-01-14 07:21:23

问题


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;

-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;
}

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" is my project name.So what's wrong here?I'm new to iPhone programming (Core Data in particular).

Can anyone help me?

Thanks...


回答1:


The problem is this line:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FoodPin" withExtension:@"momd"];

modelURL is nil meaning that the system couldn't find the resource FoodPin.momd.

Make sure you have a Core Data model in your project named FoodPin. It will appear as FoodPin.xcdatamodeld in the Project Navigator.




回答2:


I had the same problem but for me the modelURL was correctly set. The problem was that my *.xcdatamodeld file was not in the Copy bundle ressources anymore. I don't know why it disappear but to add it again fix the problem.

Here is how to fix it : You project > Build Phases > Copy Bundle ressources > "+" button and select you xcdatamodeld file




回答3:


NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"XYZ" withExtension:@"momd"];

Make sure your data model name match with URLForResource:XYZ.




回答4:


OK here's what I had to do. I had reverted by iOS version back to 9.x in order to run on an older iPad I have. This caused the error as prior versions must be case-sensitive in the bundle.




回答5:


I had the same error.

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"name" withExtension:@"momd"];

"n" in name had to be "N": like everything else in obj c this mother is case sensitive.



来源:https://stackoverflow.com/questions/32967358/cannot-load-nsmanagedobjectmodel-nil-is-an-illegal-url-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!