Let's say a have a Superclass
and an instance of this class superclassObject
.
I create a derived ClassA
.
How can I instantiate (initialize) an object classAObject
of the derived class in a such way, that all the inherited fields are equal to ones of superclassObject
?
Of course I can cycle through all the fields and manually copy the values like classAObject.property = [superclassObject.property copy]
. But the problem with this approach is that I may not know (or have access to) all the ivars/properties of the superclass.
Is there an easier (and more general) way?
It seems that I'm missing something really basic...
I'm trying to do this because I get an already initialized UIView (with frame, background color, autoresizing mask, etc.) and I want to replace it with my custom view with same parameters.
Update 1
I've found this question, and the answer there says that it
generally isn't supported in any OO language
however
In Objective-C it is possible in some cases
Ok, if it's not supported, what should I do? If it is supported, how can I achieve this?
Update 2
It seems I've found a solution to my particular case of this general problem, which I'll test and report that tomorrow.
However, this lead me to another idea: What if I use a NSCoder to encode the superclassObject
(if it implements <NSCoding>
of course), and then call [[ClassA alloc] initWithCoder:coder]
with a coder that knows data from the encoded superclassObject
? Disclaimer: Well, I'm not that familiar with coding concepts (or even not at all), so may be the last sentence is nonsense.