Cannot create an NSPersistentStoreCoordinator with a nil model

前端 未结 27 1009
囚心锁ツ
囚心锁ツ 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:14

    I just had a similar problem upgrading from IOS5 to IOS6. Turns out it was a case sensitive issue with the model name.

    Not sure if this helps anyone.

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

    I had the same problem. The solution was a mix of 2 answers:

    1) I had to add the "subdirectory" parameter into the URLForResource call

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

    2) For a reason I don't know, the data model was not included when the project was compiled. I had to add it manually into the "Build Phases / Compile resources".

    With only one of the above solutions, my application didn't work.

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

    On my side the problem was that I'd changed the case of a couple of characters in the database name.

    When starting a new project, Xcode automatically sets everything up from the project name. If you started out calling your project "Rugbyontv" and later decided to change to "RugbyOnTV" and did a search and replace, that would break it. (Credit to Rob for pointing out that the name is case-sensitive)

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

    I have been looking an answer for hours and nothing worked. But then I suddenly found this article. So according to this, the problem was in setting root controller in this part of AppDelegate.m:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        ListViewController *rootView = (ListViewController *)self.window.rootViewController;
        rootView.managedObjectContext = self.managedObjectContext;
        return YES;
    }
    

    In fact you should define what root controller will delegate your CoreData connection. And in my case I had a TabBarController connected to other views, so my targed view root controller was defined as TabBar and it caused an error. I changed

    ListViewController *rootView = (ListViewController *)self.window.rootViewController;
    

    to

    ListViewController *rootView = (ListViewController *)self.window.superview;
    

    and everything worked.

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

    Another source of this error, is that sometimes Xcode doesn't include the data model in the build.

    Check the Build Phases of your target, and ensure that the *.xcdatamodeld file is included in the Compile Sources section.

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

    I had exactly the same error message as the original post. I was wrestling with this for hours. It was this line in my AppDelegate.m.

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

    For anyone out there searching this error message and finding this thread....try this first.

    You must make sure that where it says [same with name of xcdatamodeld]....that it is!!! For some reason, mine had my project name in there and not the data model name.

    Changed it and it worked straight away.....

    Thanks to Rock & Muller for contributing.......you saved me days!!

    Gaz.

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