Search code examples
core-datansmanagedobjectnsmanagedobjectcontext

Save multiple attributes to same core data entity


I am first time user of core data and trying to learn the core data.

For start with employee example with two attributes. 1. name 2. salary

Now I done all necessary things needed for loading the data from DB. I get the values i saved before.

But problem is i am able to save only one attribute of the Employee table.For first time i saved only name of employee. Then when i got success in that, i try to save the name with salary then i got [NSManagedObject setSalary:]: unrecognized selector sent to instance 0x7472d80 exception.

Here is my code.

   - (IBAction)setBtnTouched:(id)sender {


     Employee *newEmp = [NSEntityDescription
                insertNewObjectForEntityForName:@"Employee"
                inManagedObjectContext:context];

    [newEmp setName:self.textFieldName.text];

    [emp addObject:newEmp];

/* this is the code that i added after success in saving for employee name.*/
    //[newEmp setSalary:self.textFieldSal.text];
    //[emp addObject:newEmp];
*/
     BOOL isSaved = [context save:nil];
  NSLog(@"is saved %d",isSaved);

NSLog(@"emp :%@",emp);

}

I dont understand where i went wrong because both are attributes of same entity.

Please do write to this thread.

Edited: screen shot enter image description here Regards, paggyyy123


Solution

  • Make sure your NSManagedObject subclass contains the correct attribute name accessor.

    // Employee.m
    @dynamic Salary
    

    Also, note that you are adding the new employee to your array emp twice. These two will be identical once the attributes are complete.