0
votes

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

1
Can you please add a screenshot of your core data model. - Florian Mielke
please see the edited question: Is it what you asked? - user968597
If the name of the attribute is "sal" (as in the screenshot) then you should call [newEmp setSal:...] and not [newEmp setSalary:...]. - Martin R
you mean the name of attribute and the variable name of the class that are holding these values must me same. Because i have the "Employee" class with varialbels " name" and "salary" . - user968597
@user968597: Yes, they must be identical. Note Xcode has a menu item "Editor -> Create NSManagedObject subclasses ..." to create the class files Employee.h/Employee.m, that is much less error-prone! - Martin R

1 Answers

0
votes

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.