Pregunta

I'd like to tie a core-data entity to a UITextField so that whenever the the textField finishes editing, it will automatically update the core-data object.

Here's how I'd like it work– not sure if it's possible.

  • user is a core-data entity
  • user.gender is a field of that entity
  • genderField is a UITextField
  • genderField.coreDataObject is a NSManagedObject property

    1. A user types content into a genderField
    2. On finish, the genderField fires didEndEditing
    3. In didEndEditing, the genderField sets the user.gender by saying genderField.coreDataObject = genderField.text

The problem is that the coreDataObject is the user and not user.gender. So, first of all, you can't set the coreDataObject equal to a string.

I need to store coreDataObject but then somehow set coreDataObject.gender = genderField.text

Thanks.

¿Fue útil?

Solución

You can either generate custom subclass your NSManagedObject or use key-value coding.

[genderField.coreDataObject setValue:genderField.text forKey:@"gender"]

As echoed by AlexDG, you might be better off eventually going down the generating route. But for now the above works for your needs!

See this answer for more info on generating your own classes!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top