1. 程式人生 > >Cannot create an NSPersistentStoreCoordinator with a nil model 問題解決

Cannot create an NSPersistentStoreCoordinator with a nil model 問題解決

專案中使用到coredata,由於建立時沒有選擇coredata,只好自己手動新增,

手動新增的方式就是,加個.xcdatamodel檔案之後從模板中拷貝程式碼。

拷貝程式碼後遇到了 “Cannot create an NSPersistentStoreCoordinator with a nil model ”的問題。

原因在於modelURL 沒能成功載入.xcdatamodel檔案,

調查後將NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@“****” withExtension:@"momd”];

中的momd 改為mom

,問題解決

網上搜索發現中文的相關問題的帖子不多,這裡就記一下與人方便與己方便。

程式碼如下:

- (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:@“****” withExtension:@"mom"];

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

    return _managedObjectModel;

}