0

I have a situation where I want to create an object before I know what type it will eventually be. I know what its superclass will be, and want to temporarily create a concrete instance of that superclass and allow other objects to use it in that form until its "true" class can be created.

I realize this is pretty crazy and I don't have too high expectations that this is possible, but if I could do this it would be amazing. I know the Obj-C runtime has some pretty powerful features so thought it was at least worth asking.

I've looked into object_setClass, but while this appears to allow you technically change the class of an object at runtime, it doesn't allow you to actually reallocate a new instance, complete with its own ivars, at the address of the original instance, which is really what I need as I don't know specifically what the final class will be (it needs to work with any custom subclass).

Background: My intention is to provide a placeholder object that will allow external code to register dependencies and/or hold a reference to, such that when the object is eventually filled-in, those external dependencies will still hold and they won't have to correct their references.

devios1
  • 36,899
  • 45
  • 162
  • 260
  • 1
    I'm pretty sure attempting to do this would lead to all sorts of hazards. Someone with intimate knowledge of the internals could probably do it, but it would be apt to break on the next minor update from Apple. – Hot Licks Apr 27 '15 at 20:40
  • Yeah it needs to be something that is stable and well-defined, if unconventional. – devios1 Apr 27 '15 at 20:42
  • Try explaining what you actually need to do and we can see if there is a better solution than this. – Dale Myers Apr 27 '15 at 20:51
  • @Velox Added some more background info. – devios1 Apr 27 '15 at 20:53
  • 1
    Related: [How does NSProxy "transform itself into another object"?](http://stackoverflow.com/q/6970220) – jscs Apr 27 '15 at 21:08
  • This also looks potentially promising: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html – devios1 Apr 27 '15 at 21:23
  • The problem with message forwarding is it breaks the proper chain of command of inheritance, giving precedence to the superclass over the subclass if the message happens to be understood by the superclass. If only there were a way to intercept *all* messages sent to an instance. – devios1 Apr 27 '15 at 21:25
  • It may not apply to your exact situation, but have you looked at using [objc_setAssociatedObject()](http://stackoverflow.com/questions/5909412/what-is-objc-setassociatedobject-and-in-what-cases-should-it-be-used) and objc_getAssociatedObject()? – Jeff Hay Apr 27 '15 at 22:11
  • 1
    @JeffHay Yes, those are awesome and I've used them before, but don't specifically help me in this case that I can see. – devios1 Apr 27 '15 at 22:38

1 Answers1

2

You could try using NSProxy and ultimately proxying to the "real" underlying object you need.

Tim Johnsen
  • 1,471
  • 14
  • 33