Cannot create an NSPersistentStoreCoordinator with a nil model

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

提交回复
热议问题