1
votes

i'm new to CoreData and i'm trying to store an entry of a todo list. When i try to creat a new Entry, it would crash. My entity name is "Entry", i selected "Extension" for codegen, but i didn't add any code to the subclass of Entry. what went wrong here? Thank you for helping!

    let container: NSPersistentContainer? = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer

func editOrAdd()
{
    if entry?.title != nil {
        ifNew = false   //edit entry
        updateUI()
    } else {
        ifNew = true    //new entry
        print(ifNew)

        if let context = container?.viewContext {
            let newEntry = Entry(context: context)  //crashed here
            newEntry.creatTime = Date()
            try? context.save()
            //test
            print("new entry creatTime: \(formatter((entry?.creatTime!)!))")
        }
    }
}

the complete log below

2020-02-14 23:06:43.471370+0800 PriorityList[8876:698237] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'PriorityList.Entry' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?

2020-02-14 23:06:43.475084+0800 PriorityList[8876:698237] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'PriorityList.Entry' must have a valid NSEntityDescription.'

1
Make sure Core Data is set up correctly, for instance make sure it is given the correct model name. Use “Class Definition “ for codegen, it is easier to use for a beginner. - Joakim Danielson

1 Answers

0
votes

after watching more tutorials and totally rebuilding my whole project the problem is solved.

i changed syntax of

let newEntry = Entry(context: context)  //crashed here

to

let newItem = NSEntityDescription.insertNewObject(forEntityName: "Item", into: context) as! Item

not sure what the problems are but it did worked.