【发布时间】:2017-07-22 01:55:28
【问题描述】:
我是核心数据的新手。
我正在尝试做的事情:我正在尝试创建一个 cocoatouch 框架,该框架有一个应用程序可以添加员工详细信息并将其显示在表格视图中。这样我就可以将此框架添加到我的主项目中以独立工作。
我面临的问题:框架构建没有任何错误。我已将 swift 3 的核心数据堆栈添加到框架中。但是当我运行主项目时,框架加载日志的那一刻显示“无法加载名为简单框架的模型”、“获取失败”和“员工必须具有有效的实体描述”。我在框架中使用的代码如下所示:
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)
}
【问题讨论】:
-
第一个错误表示模型文件
SimpleFramework.momd丢失或超出范围。 -
这个问题你解决了吗?我有同样的错误,我不知道如何解决它。
-
也在寻找答案,你解决了吗?
-
@Ashiq 您的核心数据模型文件位于您的应用程序或框架内的什么位置?
-
@WaelShowair 它在应用程序内部。