Cannot create an NSPersistentStoreCoordinator with a nil model

前端 未结 27 1048
囚心锁ツ
囚心锁ツ 2021-01-30 03:33

Been having my first crack at Core Data and I\'m getting the following error when running my code on my device, but it works fine on the simulator..

*

相关标签:
27条回答
  • 2021-01-30 04:11

    i got same issue with @Dominic Williams

    try to change the momd file name in below(you can find this in managedObjectModel method in default), which as same as the [file name].xcdatamodeld file you created:

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"[same with name of xcdatamodeld]" withExtension:@"momd"];
    
    0 讨论(0)
  • 2021-01-30 04:11

    See also this thread: Unit Test can't find Core Data model file

    It helped my solving the issue - which occurred only with Unit-Testing

    0 讨论(0)
  • 2021-01-30 04:12

    I've experienced a weird issue with Xcode 4.3.2 and iOS 5.

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

    returns a valid URL but

    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    

    returns a null NSManagedObjectModel. But after checking the documentation, it seems like NSManagedObjectModel needs a file where as NAME_OF_THE_MODEL.momd is a directory which contains a NAME_OF_THE_MODEL.mom file. Changing the URL to

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NAME_OF_THE_MODEL" withExtension:@"mom" subdirectory:@"NAME_OF_THE_MODEL.momd"];
    

    then works. Seems weird though that Xcode generates code that doesn't work with itself...

    0 讨论(0)
  • 2021-01-30 04:12

    I solve the problem without changing any code.

    I add the ModelName.xcdatamodeld through File->Add File instead of dragging the file into the Xcode.

     NSString *path=@"ModelName";
    
    NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"momd"]];
    
    model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    
    0 讨论(0)
  • 2021-01-30 04:13

    I tried all the solutions here, and none of them worked. My issue appeared after I renamed the project. Apparently, Xcode continues to look for the old momd file in the wrong place during compilation.

    For anyone that tried all the above solutions without success, try checking the full path of your .xcdatamodeld file. That's what worked for me.

    0 讨论(0)
  • 2021-01-30 04:14

    I had this problem out of nowhere after I deleted the generated app in the ~/Library/Application Support/iPhone Simulator. Somehow this caused subsequent builds to fail in the simulator and on devices. Hadn't changed anything to do with CoreData for ages but it would fail with the Cannot create an NSPersistentStoreCoordinator with a nil model . Tried a few things above and nothing worked.

    I could see the generated momd folder with the mom file inside. The app in the simulator could see both but it failed to generate the sqlite file.

    What solved it was adding an attribute to an entity in my xcdatamodeld file in Xcode and then immediately deleting it. I was hoping this would get Xcode to regenerate whatever was causing the issue from scratch and it seemed to work. Still not clear what was actually wrong, but my app is running in the simulator and devices again now.

    0 讨论(0)
提交回复
热议问题