0
votes

I have implemented core data migration as I needed to add a new attribute to my CoreData model.

All I basically did was:

  1. Create new model version.
  2. Edit new model version.
  3. Set the options NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption to YES upon creation of the persistentStoreCoordinator.
  4. Setting the current version to the new version.

Then I added the attribute to NSManagedObject Class:

extension Clip {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Clip> {
        return NSFetchRequest<Clip>(entityName: "Clip")
    }

    @NSManaged public var text: String?
    @NSManaged public var date: String?
    @NSManaged public var hidden: NSNumber?
    @NSManaged public var desc: String?
    @NSManaged public var pinned: NSNumber?

    @NSManaged public var tag: String?  //new
}

I am saving new items to core data like this:


    var filteredClips = [Clip]()

    func appendNewItem(text: String, desc: String?){
        let entityDescription = NSEntityDescription.entity(forEntityName: "Clip", in: managedContext)!
        let newItem = Clip.init(entity: entityDescription, insertInto: managedContext)
        newItem.text = text.trimmingCharacters(in: .whitespacesAndNewlines)
        newItem.date = getDate()
        newItem.desc = desc
        newItem.tag = "favourite" //new attribute
        newItem.hidden = false
        filteredClips.append(newItem)
        
        try! managedContext.save()
    }

Item cannot be added to the coredata Array. I'm getting this error message at launch:

"Failed to call designated initializer on NSManagedObject class 'Clip'"

Update

I don't think the error message is related to this issue as this shows up even when the migration steps are undone.

What row generates the error and filteredClips is defined to contain another type.Joakim Danielson
The error is generated at launch. filteredClips actually of type Clip(typo).unknown