Search code examples
objective-csubclassdynamic-typing

Convert object type to subclassed object type in Objective-C


Say that I have Class A and Class B. Class B is a subclass of Class A. Class A contains some properties and then Class B extends the Class A superclass by adding some additional properties, specific to that subclass. I have created a Class A object and now wish to convert the object to be a Class B type object at runtime, so that I can add data to the additional properties provided by Class B. Is there any way to do this in Objective-C? Thanks!


Solution

  • Why don't you write a constructor for Class B that takes a Class A as a argument with addition properties required by class B.

    Why don't you just create a class B in the first place? It does sound a bit strange that you'd want to do this. Perhaps you can give us some more concrete details about classes A & B and what you're doing with them.