title: How to allocate an NSObject subclass instance FROM an instance of its superclass?
body: <p>Given a class structure such as...</p>
  
  <pre><code>@interface SuperClassView : NSView                              @end
  @interface SubClassedView : SuperClassView @property int aProp; @end
  </code></pre>
  
  <p>How can one instantiate a <code>SubClassedView</code> from an <em>instance</em> of a <code>SuperClassView</code>?</p>
  
  <p>Say a method returns an instance of the superclass <code>SuperView</code>....</p>
  
  <pre><code>SuperClassView *superInstance = [ViewFactory makeSuperClassView];
  </code></pre>
  
  <p>but I <em>want</em> to get an instance of the <strong><em>subclass</em></strong> <code>SubClassedView</code>? It is not possible to simply "cast" it...</p>
  
  <pre><code>SubClassedView *subClsInstance = (SubClassedView*)[ViewFactory makeSuperClassView];
  </code></pre>
  
  <p>and there is no built-in (or easily-imagined implementation of an) <code>NSObject</code> method like</p>
  
  <pre><code>self = [super initWithInstance:[superInstance copy]];`
  </code></pre>
  
  <p>Is the only way to either copy the superclass instance's desired properties to the newly instantiated subclass object, like...</p>
  
  <pre><code>SubClassedView *subClsInstance = SubClassedView.new;
  for (NSString* someKey in @["frame",@"color",@"someOtherProperty])
     [subClsInstance setValue:[superInstance valueForKey:someKey] forKey:someKey];
  </code></pre>
  
  <p>Or add (<em>swizzle at runtime</em>) the subclass' "additional property methods" (in this case <code>setAProp:</code> and <code>aProp</code>) to the superclass instance (and also cast it upwards)...</p>
  
  <pre><code>SubClassedView *subClsInstance = (SubClassedView*)[ViewFactory makeSuperClassView];
  [subClsInstance addSwizzleMethod:@selector(setAProp:) viaSomeMagic:....];
  [subClsInstance addSwizzleMethod:@selector(aProp)     viaSomeMagic:....];
  </code></pre>
  
  <p>Hopefully this is an easy <em>runtime trick</em> that I simply don't know... not a sad sign that I am haplessly trying to trick <code>ObjC</code> into multiple-inheritance via some <em>embarrassing anti-pattern</em>. Either way, ideas?</p>
  
  <p><strong>EDIT:</strong> Pending @BryanChen posting his comment as an answer this is achieved easily via his suggested runtime function, or as a category on <code>NSObject</code> á la..</p>
  
  <pre><code>@implementation NSObject (SettingClass)
  - (void)setClass:(Class)kls { if (kls) object_setClass(self, kls); } @end
  </code></pre>