Обновление одного объекта в массиве (Transformable) из базовых данных Swift 4 - PullRequest
0 голосов
/ 21 апреля 2020

У меня есть объект с именем «Контакты» с атрибутом «contactList» с типом «Transformable». Я пытался сохранить и удаление работает нормально. но обновление значения определенной записи не работает

Вот мой код

   func updateRecord(primaryKey:String, mobileNumber:String) {
            if let appDelegate = UIApplication.shared.delegate as? AppDelegate{
                let context = appDelegate.persistentContainer.viewContext
                let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Contacts")

                do{
                    let objects = try context.fetch(fetchRequest)
                    if objects.count > 0{
                        for object in objects{
                            let match = object as! NSManagedObject
                            let list = match.value(forKey: "contactList") as? [String:Any]
                            if list != nil && !(list!.isEmpty) {
                                let key = list!["taskPrimaryKey"] as! String
                                if key == primaryKey{
                                  match.setValue(mobileNumber, forKeyPath: "Mobile")

                                    do {
                                        try context.save() // <- remember to put this :)
                                    } catch {
                                        // Do something... fatalerror
                                        print("fatalerror : \(error.localizedDescription)")
                                    }
                                    break
                                }
                            }
                        }

                    }
                } catch {
                    print("could not retrive error : \(error.localizedDescription)")
                }

            }
    }

1 Ответ

0 голосов
/ 23 апреля 2020

Я предполагаю, mobileNumber это number ??

match.setValue(NSNumber(mobileNumber), forKeyPath: "Mobile")
...