title: Problems casting NSManagedObject to subclass type
body: <p>I'm using custom classes for my CoreData stack. Class properties are set properly in the model. There's some points in the app that are abstracted to use setValue... on an NSManagedObject, but I have a few cases where that fails with an NSInvalidArgumentException, specifically when setting a related object; error indicates it wants the specific type, and it's' getting an NSManagedObject, hence the error. So, I thought I would just take the short route and cast my instance prior to the offending call, if it's of a certain entity; like this:</p>
  
  <pre><code>NSManagedObject *addressObject = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:[object managedObjectContext]];
      if ([[[object entity]name] isEqualToString:@"Hospital"]) {
          Contact *contact = (Contact*)object;
          DLog(@"The class of contact is: %@", [contact class]);
          contact.Address = addressObject;
  
      }
      else{
          [object setValue:addressObject forKey:@"Address"];
      }
  </code></pre>
  
  <p>I know, Address shouldn't be capitalized; I inherited this mess... anyway, I would totally expect that the contact object is a Contact, but it's not, it's an NSManagedObject! What am I doing wrong with the cast? Everything I've uncovered says this is the right way to cast, but for some reason, it's not working for me here. Of course, this wouldn't be necessary if the addressObject didn't complain about getting an NSManagedObject instead of a Contact (sorry, Hospital inherits from Contact here), and that's another baffling thing, but first things first. How can I coerce object to type Contact, which it really is?</p>
  
  <p>Here's the relevant trace:</p>
  
  <p><em>*</em> Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-one relationship: property = "Contact"; desired type = Contact; given type = NSManagedObject; value = ...</p>
  
  <p>For completeness sake, the Address class has declaration for Contact as:</p>
  
  <pre><code>@property (nonatomic, retain) NSManagedObject * Contact;
  </code></pre>
  
  <p>with implementation for Contact as the normal dynamic, like:</p>
  
  <pre><code>@dynamic Contact;
  </code></pre>
  
  <p>Maybe I need some sleep? ;-) Thanks</p>