-3

I have a function which errors where marker // ERRORS HERE

The db is empty, I try to append a Persistence record, then set its values,

tasks[0].persistencevalue = "Some Text"!

Any help would be appreciated.

Thanks

The stack shows :

2017-08-18 10:09:07.158047+0100 Ontrack[6491:1684506] [error] error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'Persistence' CoreData: error: CoreData: error: Failed to call designated initializer on NSManagedObject class "Persistence" 2017-08-18 10:09:08.678 Ontrack[6491:1684506] -[Persistence setPersistencevalue:]: unrecognized selector sent to instance 0x60800026a040 2017-08-18 10:09:08.681 Ontrack[6491:1684506] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Persistence setPersistencevalue:]: unrecognized selector sent to instance 0x60800026a040'

func Update() {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let app = (UIApplication.shared.delegate as! AppDelegate )        

    var tasks: [ Persistence] = []    

    let pers = Persistence()

    tasks.append(pers)
    cnt = tasks.count
    // ERRORS HERE 
    tasks[0].persistencevalue = Rate.text!
    // END ERROR
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Jeff
  • 1
  • Possible duplicate of [CoreData Swift: How to save and load data?](https://stackoverflow.com/questions/25586593/coredata-swift-how-to-save-and-load-data) – Ujesh Aug 18 '17 at 09:32
  • See also [this answer](https://stackoverflow.com/a/33307824/3985749) to the "Failed to Call Designated initializer" error. – pbasdf Aug 19 '17 at 12:25

1 Answers1

0

Thanks for your help, I was able to fix the problem with the following code. Hope it helps someone with the same problem.

func Update() {

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let app = (UIApplication.shared.delegate as! AppDelegate )

    var persistencies: [ Persistence]?


    var cnt = persistencies?.count

    var perRec: AnyObject! = NSEntityDescription.insertNewObject(forEntityName: "Persistence", into: context) as NSManagedObject

    perRec.setValue(DbKeys.PERSISTENCE_SPEECHRATE_KEY, forKey: "persistencekey")
    perRec.setValue(Rate.text, forKey: "persistencevalue")

    do {
     try context.save()
    } catch {
        print (" ERROR")
    }

    app.saveContext()

    do {
        persistencies = try context.fetch(Persistence.fetchRequest())
    } catch {
        print("Fetching Failed")
    }


    cnt = persistencies?.count

  }
Jeff
  • 1