核心数据:加载模型失败 [英] Core data: Failed to load model
问题描述
我是核心数据的新手.
我想要做什么:我正在尝试创建一个 cocoatouch 框架,该框架有一个应用程序来添加员工详细信息并在表格视图中显示它们.这样我就可以将这个框架添加到我的主项目中以独立工作.
What I am trying to DO: I am trying to create a cocoatouch framework that has an app to add employee details and display them in a table view. So that i can add this framework to my main project to work independently.
我面临的问题: 框架构建没有任何错误.我已将 swift 3 的核心数据堆栈添加到框架中.但是当我运行主项目时,框架加载日志的那一刻显示无法加载名为简单框架的模型"、获取失败"和员工必须具有有效的实体描述".我在框架中使用的代码如下所示:
Issues I face: The frame work builds without any error. I have added the core data stack from swift 3 to the framework. But when i run the main project, the moment the framework loads the log displays "Failed to load model named Simple framework", "fetch failed" and "employee must have a valid entity description". The code that I have used in the framework is as shown below :
public class CoreDataStack {
public static let sharedInstance = CoreDataStack()
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "SimpleFramework")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
fatalError("Unresolved error (error), (error)")
}
})
return container
}()
public func saveContext() {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch let error as NSError {
fatalError("Unresolved error (error), (error.userInfo)")
}
}
}
}
@IBAction func addEmployee(_ sender: Any) {
//To save the data
let context = CoreDataStack.sharedInstance.persistentContainer.viewContext
let employee = Employee(context: context)
employee.employeeName = nameTextField.text
employee.employeeAge = Int16(ageTextField.text!)!
employee.hasVehicle = hasVehicle.isOn
CoreDataStack.sharedInstance.saveContext()
navigationController!.popViewController(animated: true)
}
@IBAction func addEmployee(_ sender: Any) {
//To save the data
let context = CoreDataStack.sharedInstance.persistentContainer.viewContext
let employee = Employee(context: context)
employee.employeeName = nameTextField.text
employee.employeeAge = Int16(ageTextField.text!)!
employee.hasVehicle = hasVehicle.isOn
CoreDataStack.sharedInstance.saveContext()
navigationController!.popViewController(animated: true)
}
推荐答案
我遇到了这个问题,当我有错误的模型名称时 - 应该是模型名称,而不是项目(参见屏幕截图)
I've had this issue, when I had wrong model name - it should be models name, not the projects (see the screen shot)
这篇关于核心数据:加载模型失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!