title: objective c type casting an object based on an NSString class name
body: <p>I have an issue where I am getting in an object of type id. i know its base type is an NSManagedObject but the custom attributes for whatever type it actually is (name, email, etc) are what i really need. in the long run, i am trying to make one method that will turn my custom NSManagedObjects into NSDictionaries. maybe i am going down the wrong path in general but....</p>
  
  <p>If i have something like </p>
  
  <pre><code>-(void)someMethod:(id)obj{
  ...
  Class someClass = NSClassFromString([[obj class] description]);
  ...
  </code></pre>
  
  <p>i can get the class from that but then i cannot really figure out how to cast my object as that class.</p>
  
  <p>ideally this would be the next few lines of that code</p>
  
  <pre><code>someClass* myObject = (someClass*)obj;
  NSArray *keys = [[[myObject entity] attributesByName] allKeys];
  NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];
  //do something with dict
  ...
  </code></pre>
  
  <p>but i cannot figure out how to get an object of the type someClass. I have seen where you can use the NSClassFromString object for an alloc init call like</p>
  
  <pre><code>id someObject = [[className alloc] init];
  </code></pre>
  
  <p>but that will not work for this situation as i already have an object passed in that I need. any ideas / criticisms? I am really trying to avoid a crap load of if statements for all of my items but it may be the quickest way right now...</p>