Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swift - Core data: Failed to load model

I am new to core data.

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.

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)
}

This is a screenshot of the console log.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've had this issue, when I had wrong model name - it should be models name, not the projects (see the screen shot)enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...