I'm trying to update some of the properties of my Core Data model using .setValue(value:, forKeyPath:)
method. On my model, I have one entity called "Alumno", which has an attribute called "especialidadRelacionada" related to another entity, called "Especialidad". It's a many-to-one relationship, as an "Alumno" can only has one "Especialidad", although an "Especialidad" can be matched to many "Alumnos". "Especialidad" entity has, as well, "nombre" as its main attribute. Here the two swift files, as generated from Xcode, from deep inside developer folders:
extension Alumno {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Alumno> {
return NSFetchRequest<Alumno>(entityName: "Alumno")
}
@NSManaged public var correo: String?
@NSManaged public var foto: Data?
@NSManaged public var movil: String?
@NSManaged public var nombre: String?
@NSManaged public var repertorio: String?
@NSManaged public var cursoRelacionado: Curso?
@NSManaged public var ensayosRelacionados: NSSet?
@NSManaged public var especialidadRelacionada: Especialidad?
@NSManaged public var tutorRelacionado: Tutor?
}
extension Especialidad {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Especialidad> {
return NSFetchRequest<Especialidad>(entityName: "Especialidad")
}
@NSManaged public var nombre: String?
@NSManaged public var alumno: NSSet?
@NSManaged public var obra: NSSet?
}
I get the string for "Especialidad" to be set from a text field user writes in:
@IBOutlet weak var especialidadField: UITextField!
But, when I try to UPDATE "Especialidad" data (previously saved to Core Data with no problem at all), with user's written string:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
alumno?.setValue(especialidadField.text, forKey: "especialidadRelacionada.nombre")
do {
try context.save()
} catch {
...
}
I get this error: "Failed to call designated initializer on NSManagedObject class". It seems that something is incorrect with my "forKey" string, but I can't find what, after so much time looking for it on the internet.
Thanks!