NSManagedObjectModel initWithContentsOfURL 返回 nil,即使 modelURL 有效

发布于 2024-11-25 13:36:52 字数 982 浏览 0 评论 0原文

即使路径正确,我的 NSManagedObjectModel 返回 nil 。

        NSString *modelKey = [NSString stringWithFormat:@"/%@/Model", name];
    NSString *modelPath = [((Configuration *)[Configuration shared]) stringEntry:modelKey];
    NSURL *modelURL = nil;
    if ( ! [modelPath contains:@"://"] ) {
        modelPath = PathForBundleResource( modelPath );
        modelURL = [NSURL fileURLWithPath:modelPath];
    }
    NSManagedObjectModel *m = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSString *PathForBundleResource(NSString *relativePath)

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
return [resourcePath stringByAppendingPathComponent:relativePath];

我已经重置了模拟器,进行了干净的构建,但没有任何帮助。

我是 iPhone 编程新手(特别是核心数据)。

将不胜感激任何帮助。

编辑:我编辑了有关 xcdatamodeld 文件的原始帖子。最初没有正确连接,但现在连接正确并且仍然存在相同的问题。

编辑2:显然,链接后xcdatamodel的名称与xcdatamodel的名称有一些变化。现在正在工作。感觉自己好傻啊。不知道怎么删除这个问题。

my NSManagedObjectModel is returning nil eventhough the path is correct.

        NSString *modelKey = [NSString stringWithFormat:@"/%@/Model", name];
    NSString *modelPath = [((Configuration *)[Configuration shared]) stringEntry:modelKey];
    NSURL *modelURL = nil;
    if ( ! [modelPath contains:@"://"] ) {
        modelPath = PathForBundleResource( modelPath );
        modelURL = [NSURL fileURLWithPath:modelPath];
    }
    NSManagedObjectModel *m = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSString *PathForBundleResource(NSString *relativePath)

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
return [resourcePath stringByAppendingPathComponent:relativePath];

I've reset the simulator, did a clean build but nothing helped.

I'm new to iPhone programming (core data in particular).

Would appreciate any help.

Edit: I've edited the original post regarding the xcdatamodeld file. It wasn't linked up properly initially but now it is and still having the same problem.

Edit 2: Apparently the name of the xcdatamodel had some variations to the name of the xcdatamodel after linkage. It's working now. Feeling so stupid. Don't know how to delete this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡紫姑娘! 2024-12-02 13:36:52

重命名 .xcdatamodeld 文件后,我遇到了同样的问题。通过关闭并重新打开 Xcode 客户端解决了该问题。

I had the same problem after I renamed .xcdatamodeld file. The problem was solved by closing and reopening the Xcode client.

花开柳相依 2024-12-02 13:36:52

我刚刚在不区分大小写的文件系统上更改 .xcdatamodeld 文件名称的大小写(例如将“mydatastore.xcdatamodeld”重构为“MyDatastore.xcdatamodeld”)后遇到此问题。

该修复包括上述两个答案:

  1. 将数据模型文件重构为明显不同的名称,例如“MyDatastoreTemporary.xcdatamodeld”

  2. 产品>清洁

  3. 关闭 XCode。重新打开 XCode 和项目。

  4. 将数据模型文件重构回其最终名称,例如“MyDatastore.xcdatamodeld”

  5. Product >清洁

  6. 关闭 XCode。重新打开 XCode 和项目。

I just encountered this issue after changing the case of the name of the .xcdatamodeld file (e.g. refactored "mydatastore.xcdatamodeld" to "MyDatastore.xcdatamodeld") on a case-insensitive filesystem.

The fix includes both of the above answers:

  1. Refactor the data model file to a distinctly different name, e.g. "MyDatastoreTemporary.xcdatamodeld"

  2. Product > Clean

  3. Close XCode. Re-open XCode and the project.

  4. Refactor the data model file back to its final name, e.g. "MyDatastore.xcdatamodeld"

  5. Product > Clean

  6. Close XCode. Re-open XCode and the project.

暖伴 2024-12-02 13:36:52

我尝试了上述两个答案,但是为我解决的问题是从 Xcode 项目中删除并重新添加 .xcdatamodeld 文件。无需清洁。由于某种原因,将其添加回来似乎可以解决问题。

I tried both of the above answers, however what fixed it for me was removing and re-adding the .xcdatamodeld file from/to the Xcode project. No cleaning was necessary. For some reason adding it back seems to fix the problem.

紙鸢 2024-12-02 13:36:52

我解决了问题,将模型 .xcdatamodeld 移至垃圾箱,然后再添加一次。

I resolved the problem moved to trash the model .xcdatamodeld and then added it one more time.

心不设防 2024-12-02 13:36:52

如果您尚未对模型进行版本控制,则迄今为止最安全的方法是执行以下操作。

  1. 不要重命名您的 xcdatamodeld 文件。
  2. 相反,使用新名称创建一个全新的数据模型文件。
  3. 保持文件为空并退出 Xcode
  4. 打开终端并 cd (更改目录)到旧的 xcdatamodeld 目录。
  5. 该目录中应该有一个以 xcdatamodel 结尾的目录。更改到此目录。
  6. 在 xcdatamodel 目录中,您将找到一个名为 contents 的文件。将此文件复制到新命名的 ?.xcdatamodeld/?.xcdatamodel 目录下的同一位置。
  7. 打开 Xcode 并修改您的 Core Data 代码以访问这个新模型。
  8. 构建并测试。
  9. 从项目中删除旧的 xcdatamodeld。

这适用于没有版本的基本模型。

The by far safest way, if you have not yet versioned your model, is to do the following.

  1. Do not rename your xcdatamodeld file.
  2. Instead, create a brand new Data Model file with the new name.
  3. Keep the file empty and quit Xcode
  4. Open up a terminal and cd (change directory) into your old xcdatamodeld directory.
  5. Inside this directory should be a directory ending in xcdatamodel. Change into this directory.
  6. Inside the xcdatamodel directory you will find a file named contents. Copy this file into the same location under your newly named ?.xcdatamodeld/?.xcdatamodel directory.
  7. Open up Xcode and modify your Core Data code to access this new model.
  8. Build and test.
  9. Remove the old xcdatamodeld from your project.

This works with basic models that have no versions.

堇色安年 2024-12-02 13:36:52

当我第一次运行该项目时,我将扩展名指定为xcdatamodeld。但那次失败了,应该是momd。但我通过了一个 nil 而不是 momd 假设扩展不是强制性的。但是对于 nil,initWithContentsOfURL 失败了,当我最终输入 momd 时,它工作得很好。

When I ran the project the first time, I specified the extension as xcdatamodeld. But that failed since, it should be momd. But I passed a nil instead of momd assuming extension won't be mandatory. But with nil, the initWithContentsOfURL failed and when I finally entered momd,it worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文