NSManagedObjectModel initWithContentsOfURL 返回 nil,即使 modelURL 有效
即使路径正确,我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
重命名 .xcdatamodeld 文件后,我遇到了同样的问题。通过关闭并重新打开 Xcode 客户端解决了该问题。
I had the same problem after I renamed .xcdatamodeld file. The problem was solved by closing and reopening the Xcode client.
我刚刚在不区分大小写的文件系统上更改 .xcdatamodeld 文件名称的大小写(例如将“mydatastore.xcdatamodeld”重构为“MyDatastore.xcdatamodeld”)后遇到此问题。
该修复包括上述两个答案:
将数据模型文件重构为明显不同的名称,例如“MyDatastoreTemporary.xcdatamodeld”
产品>清洁
关闭 XCode。重新打开 XCode 和项目。
将数据模型文件重构回其最终名称,例如“MyDatastore.xcdatamodeld”
Product >清洁
关闭 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:
Refactor the data model file to a distinctly different name, e.g. "MyDatastoreTemporary.xcdatamodeld"
Product > Clean
Close XCode. Re-open XCode and the project.
Refactor the data model file back to its final name, e.g. "MyDatastore.xcdatamodeld"
Product > Clean
Close XCode. Re-open XCode and the project.
我尝试了上述两个答案,但是为我解决的问题是从 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.我解决了问题,将模型 .xcdatamodeld 移至垃圾箱,然后再添加一次。
I resolved the problem moved to trash the model .xcdatamodeld and then added it one more time.
如果您尚未对模型进行版本控制,则迄今为止最安全的方法是执行以下操作。
contents
的文件。将此文件复制到新命名的 ?.xcdatamodeld/?.xcdatamodel 目录下的同一位置。这适用于没有版本的基本模型。
The by far safest way, if you have not yet versioned your model, is to do the following.
contents
. Copy this file into the same location under your newly named ?.xcdatamodeld/?.xcdatamodel directory.This works with basic models that have no versions.
当我第一次运行该项目时,我将扩展名指定为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.