xcode 从设备中删除应用程序后,无法使用 nil 模型创建 NSPersistentStoreCoordinator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12617585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:39:59  来源:igfitidea点击:

Cannot create an NSPersistentStoreCoordinator with a nil model after deleting app from device

iphoneiosxcodecore-data

提问by amb

I'm receiving an 'Cannot create an NSPersistentStoreCoordinator with a nil model'error after deleting my application from device. I'm testing an iPhone app in an iPad device. I've put this code to check if I have the file in AppDelegate.m:

'Cannot create an NSPersistentStoreCoordinator with a nil model'从设备中删除我的应用程序后,我收到一条错误消息。我正在 iPad 设备上测试 iPhone 应用程序。我已经把这个代码放在检查我是否有文件AppDelegate.m

- (NSManagedObjectModel *)managedObjectModel {
    if (__managedObjectModel != nil) {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Name" withExtension:@"momd"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:[modelURL path]]) {
        NSLog(@"%@", [modelURL path]); //This is printed because file exists
    }
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

The problem is that [NSManagedObjectModel initWithContentsOfURL]is returning nil value. I've done the following things, with no success:

问题是[NSManagedObjectModel initWithContentsOfURL]返回 nil 值。我做了以下事情,但没有成功:

  1. Change managedObjectModel instantiation with this __managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
  2. Cleaned Build Folder and Cleaned project
  3. Restarted Xcode
  4. Restarted computer
  5. Changed "momd" to "mom"
  6. .xcdatamodeldis in Copy Bundle Resources and Compile Sources
  7. Renamed .xcdatamodeldand cleaned and closed Xcode project several times
  8. Turned off and on the device
  9. Deleted folders from: $ cd /Users/john/Library/Developer/Xcode/DerivedData
  10. Changed sqlite name for forcing database generation
  11. Deleted (again) application from devine
  1. 使用此更改 managedObjectModel 实例化 __managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
  2. 清理构建文件夹和清理项目
  3. 重启Xcode
  4. 重启电脑
  5. 把“妈妈”改成了“妈妈”
  6. .xcdatamodeld在复制捆绑资源和编译源中
  7. .xcdatamodeld多次重命名、清理和关闭 Xcode 项目
  8. 关闭和打开设备
  9. 从以下位置删除的文件夹: $ cd /Users/john/Library/Developer/Xcode/DerivedData
  10. 更改了用于强制数据库生成的 sqlite 名称
  11. 从 devine 删除(再次)应用程序

I've been searching the solution for hours, and I still cannot find it.

我已经搜索了几个小时的解决方案,但我仍然找不到它。

回答by amb

Finally, after two days trying to solve this issue, I've found the solution here:

最后,经过两天的尝试解决这个问题,我在这里找到了解决方案:

How to create the magic .xcdatamodeld folder / package?

如何创建神奇的 .xcdatamodeld 文件夹/包?

I'm now finishing a project that other developer started, and it seems that he didn't pushed the latest changes to the repo, but those changes were in the app in the device, and when I removed the app I deleted the right .xcdatamodeldfile. The problem was that I had just a MyApp.xcdatamodelfile in the project, and this was the reason of having a momdempty folder, it seems.

我现在正在完成其他开发人员启动的项目,似乎他没有将最新更改推送到 repo,但这些更改在设备中的应用程序中,当我删除应用程序时,我删除了正确的.xcdatamodeld文件. 问题是我MyApp.xcdatamodel在项目中只有一个文件momd,这似乎是有一个空文件夹的原因。

In order to create the right hierarchy of data model, the solution was quite easy:

为了创建正确的数据模型层次结构,解决方案非常简单:

  1. Select the MyApp.xcdatamodel
  2. Go to Editor > Add Model Version...
  1. 选择 MyApp.xcdatamodel
  2. 转到编辑器 > 添加模型版本...

And this embedded the MyApp.xcdatamodelfile into MyApp.xcdatamodeld. Now the momdfolder has the momfiles and the app runs OK. The only problem now is that I have two MyApp.xcdatamodel, one with a green selected icon, but both with the same content so no problem.

这将MyApp.xcdatamodel文件嵌入到MyApp.xcdatamodeld. 现在momd文件夹中有mom文件,应用程序运行正常。现在唯一的问题是我有两个MyApp.xcdatamodel,一个带有绿色选定的图标,但两者的内容相同,所以没问题。

回答by Brayan Acebo

I backed to build my first iOS app again. Today, I got this error:'Cannot create an NSPersistentStoreCoordinator with a nil model'. Actually, that is easy to fix. Please make this line of code:

我再次支持构建我的第一个 iOS 应用程序。今天,我收到了这个错误:'不能用 nil 模型创建一个 NSPersistentStoreCoordinator'。实际上,这很容易解决。请制作这行代码:

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

The [name] you filled in is same as your model file (.xcdatamodeld). For example I got a TipRecord.xcdatamodeld then this line should be:

您填写的 [name] 与您的模型文件 (.xcdatamodeld) 相同。例如我得到了一个 TipRecord.xcdatamodeld 那么这一行应该是:

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

回答by Bajaj

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

select exact url for resource name..

选择资源名称的确切网址..

回答by Mario Gil

it's relatively simple.

它相对简单。

if you have an xcdatamodel file withoutversions: use the extension "mom" in your managedObjectModel declaration. If your xcdatamodel has versions, you should use "momd".

如果您有一个没有版本的 xcdatamodel 文件:在您的 managedObjectModel 声明中使用扩展名“mom”。如果您的 xcdatamodel 有版本,您应该使用“momd”。