【发布时间】:2015-07-13 06:41:18
【问题描述】:
我创建了一个应用并在其中启用了 Core Data,它正在尝试使用以下代码加载托管对象模型:
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource("MyAppName", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
但在运行时,我遇到了一个致命错误,因为我强制解开一个 nil 的可选:我的应用程序包中没有 MyAppName.momd。
取而代之的是MyAppName.mom,我更改了modelURL 的扩展名,现在它可以正常工作了。
但是为什么会出现这个问题?我是 Core Data 的新手,但我最后一次尝试它时从未遇到过这个问题!为什么我的项目的文件扩展名发生了变化?
如何确定在加载托管对象模型时,它将是具有良好扩展名的模型?如果你想发布一个应用程序,不知道你必须期望什么文件扩展名并不是一种真正令人放心的感觉!
【问题讨论】: