Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is definitely possible using ObjC runtime, but it will be a bit hairy... This is a link to Apple's docs: <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html" rel="nofollow">Objective-C Runtime</a> and an example of its usage: <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html" rel="nofollow">Objective-C Runtime Programming</a>.</p> <p>You would iterate over all properties, check their attributes to find out those you can set (i.e. omitting readonly properties), get their getter method on the superclass, read value and set using setter method on the subclass.</p> <p>In the specific case of UIView this may work - you will tweak until it works. Generally it may be difficult: what about ivars that are not exposed as properties? Do you want to copy them all? Then you would need to check properties defined in the class and all protocols the class implements. And I'm not sure if won't be necessary to iterate over all the superclasses of your superclass to get all defined properties and ivars.</p> <p>But, still, it is possible.</p> <p><strong>UPDATE</strong></p> <p>If you would copy instance variables then probably you would be probably set without even touching properties - I assume that all state is saved in ivars eventually. So in this case also no need to touch protocol defined properties. </p> <p>But the other solution that may work well enough is to just stick to the public interface (well actually part of it: just what appears to be data and not functionality): read data through properties of the superclass and set using properties of the derived class, again assuming that the classes will do the right thing with the data. In this case the protocol properties are equally important because the are also part of the public interface and their implementation can save state into instance variables.</p> <p>Again, I wouldn't try this approach as a general solution to copy any class but in a case of duplicating the data of one specific class that might work well enough and you can easily and thorough test your case.</p>
    singulars
    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.
    1. This table or related slice is empty.
    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