title: How to set a reference from one object to another?
body: <p><strong>SOLVED</strong></p>
  
  <p>I misspelled the property department -> Department.
  Thanks for the answers and  a big <strong>SORRY</strong></p>
  
  <p>Hi,</p>
  
  <p>Imagine the obvious model of employees and departments, where each employee must refer to a single department, and each department may have none to many employees.</p>
  
  <p>For Employee I have a pointer to Department, and I set its value to the address of the object of the relevant Department.</p>
  
  <p>For Department I have a NSSet, to which I can add employees.</p>
  
  <p>The problem is that when I create a new employee and a new department, the objects exist in the memory at some place. The reference from Employee to Department, and vice versa, contains these addresses. When I'll fetch the data it will be located at a different location. So it is not clear how the connection between these objects is done.</p>
  
  <p>Here is the code as I understand it should be:</p>
  
  <pre><code>    // Fetch Plist object
      NSDictionary *plistDictionary = [[NSDictionary alloc] initWithDictionary:plistData];
  
      // Create Department Entity
      Department *department = (Department *)[NSEntityDescription 
                                  insertNewObjectForEntityForName:@"Department" 
                                  inManagedObjectContext:managedObjectContext];
      // Set properties
      [department setName:[plistDictionary valueForKey:@"department"]];
  
      // Create Employee entity
      Employee *employee = (Employee *)[NSEntityDescription 
                               insertNewObjectForEntityForName:@"Employee" 
                               inManagedObjectContext:managedObjectContext];
  
      // Set properties
      [employee setFileName:[plistDictionary valueForKey:@"picture"]];
      [employee setName:[plistDictionary valueForKey:@"name"]];
  
      // Set relationship
      [employee setDepartment:department];
      [department addEmployee:employee];
  
      // Save data to core data
      [self saveAction];
  </code></pre>
  
  <p>And here is the Error I get:</p>
  
  <pre><code>      2010-04-08 16:06:11.295 Paparazzi2[2656:207] ERROR:saveAction. Unresolved Core Data Save error Error Domain=NSCocoaErrorDomain Code=1570 UserInfo=0x3d1c6f0 "Operation could not be completed. (Cocoa error 1570.)", {
  NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1570.)";
  NSValidationErrorKey = department;
  NSValidationErrorObject = &lt;Employee: 0x3d13a30&gt; (entity: Employee; id: 0x3d16670 &lt;x-coredata:///Employee/t8C950118-C388-4020-8CD9-1F49138A94193&gt; ; data: {
  picture = "mike.jpg";
  name = "Mike Smith";
  department = nil;
  </code></pre>
  
  <p>});
  }</p>
  
  <p>Thanks,
  Tzur.</p>