Note that there are some explanatory texts on larger screens.

plurals
  1. POInitialize an object with a superclass' instance
    primarykey
    data
    text
    <p>Let's say a have a <code>Superclass</code> and an instance of this class <code>superclassObject</code>.<br> I create a derived <code>ClassA</code>. </p> <p>How can I instantiate (initialize) an object <code>classAObject</code> of the derived class in a such way, that all the inherited fields are equal to ones of <code>superclassObject</code>? </p> <p>Of course I can cycle through all the fields and manually copy the values like <code>classAObject.property = [superclassObject.property copy]</code>. 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?</p> <p>It seems that I'm missing something really basic...</p> <p>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.</p> <h3>Update 1</h3> <p>I've found <a href="https://stackoverflow.com/questions/4872155/how-to-initialize-an-object-of-subclass-with-an-existing-object-of-superclass-i">this question</a>, and the answer there says that it</p> <blockquote> <p>generally isn't supported in any OO language</p> </blockquote> <p>however</p> <blockquote> <p>In Objective-C it is possible in some cases</p> </blockquote> <p>Ok, if it's <strong>not</strong> supported, what should I do? If it <strong>is</strong> supported, how can I achieve this?</p> <h3>Update 2</h3> <p>It seems I've found a solution to my particular case of this general problem, which I'll test and report that tomorrow.</p> <p>However, this lead me to another idea: What if I use a NSCoder to encode the <code>superclassObject</code> (if it implements <code>&lt;NSCoding&gt;</code> of course), and then call <code>[[ClassA alloc] initWithCoder:coder]</code> with a coder that knows data from the encoded <code>superclassObject</code>? <em>Disclaimer:</em> Well, I'm not that familiar with coding concepts (or even not at all), so may be the last sentence is nonsense.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload