1

For example:

@implementation MyClass{
    NSNumber *something;
}

I would like to be able to add a few other like it at runtime to a particular object (just one instance), not the entire class. They already have pointers to other objects in them and I need MyClass to be able to use them.

The use case would be like this. I'm in another instance, call it A and I have some variables and a method, I would like to inject the variables and method into B, execute the method and them detach them, so object B is pristine.

*In the case of the method I would preferably not class_addMethod it and would prefer to just send the implementation to Object A to execute it on itself.

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
unom
  • 11,438
  • 4
  • 34
  • 54
  • So what has changed after you 'detach' them? The method edited some of the 'original' instance variables? – Wain Mar 29 '14 at 21:42
  • Yes. Object A's own original variables have been modified. – unom Mar 29 '14 at 21:42
  • 3
    No, you can't do this. You need to create an entirely new class (which could just be a subclass of `MyClass` with a few more iVars), as once the class is registered with the run time, it cannot have ivars added to it. Methods can be added to any class at any time, however. – Richard J. Ross III Mar 29 '14 at 21:43
  • Perhaps you could use associated objects, though? They provide a similar construct to instance variables. – Richard J. Ross III Mar 29 '14 at 21:44
  • 2
    It's very likely possible with Objective-C -- there are a number of really weird functions you can use to modify a class or object at runtime -- but the interfaces are quite arcane and "dangerous", so you'd be better advised to find a different technique. – Hot Licks Mar 29 '14 at 21:50
  • I've read the documentation there's class_addIvar and a lot of others, but I was looking at a technique to give me "per object" control. I can do something like class_addIvar then object_setIvar and class_addMethod then method_invoke... but how do I remove them... – unom Mar 29 '14 at 23:03
  • For some techniques that give you per-instance methods, see [Creating delegates on the spot with blocks](http://stackoverflow.com/q/15438410). Richard is right on about the ivars, though. – jscs Mar 29 '14 at 23:37
  • **"inject .. detach"**. Makes no sense. Simply have a property "x". Set it as you wish. When you want to .. set it to nil. "x" could be itself a large struct or a large class with a lot of information. – Fattie Mar 30 '14 at 14:54
  • Note that if you are (incredibly) truly dealing with profoundly high performance computing, and simply working with raw memory, you could do things "sort of like" what you describe; but it's inconceivable that is the case here. – Fattie Mar 30 '14 at 14:56

1 Answers1

0

You can use associated objects (see objc_setAssociatedObject() and related functions), but adding ivars at runtime is a fairly uncommon practice. What are you trying to accomplish?

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • For example, I have 10 completely different classes of objects. They need to interact, 5-6 at a time and their respective variables change in accordance with the composition of the interaction. I need to make every object aware of all the others at runtime. How would one use objc_getAssociatedObject(). I've just read you can fake Ivars if you mask them behind a fake getter and setters. Could in theory store in each object an association to all the others, I essentially need to store the object itself, the role it has in the composition and a list of blocks i can execute on it. – unom Mar 29 '14 at 23:25
  • It sounds like you simply need to get in to **KVO**, an incredibly useful part of the whole Cocoa experience. – Fattie Mar 30 '14 at 14:52