Is there defined behavior for modifying an Objective-C isa?

In Objective-C, you can change the dynamic type of an object at run time by assigning it a member variable isa:

id object = ...;
object->isa = [SomeClass class];

Is this behavior undefined? I am currently doing this as a kludge for something else, and it seems to work, but I feel so dirty doing it like that. The new class that I am installing does not add any member variables, it just overrides one method and adds a new one, so the size of the class is the same. I feel that if I resized the object, I would get a lot of bad things.

+5
source share
4 answers

I think that, as you say, is dirty.

, , :

  • .
  • -.

, , Objective-C . , .

+1

Objective-C : object_setClass. , , ( ). , , .

+1

, - .

0
source

This answer by Joshua Weinberg gives the right way for this. This worked for me. :)

And Darron was right with his educated guess :) it will work only if it is a superclass of the current object, and if you do not add any members.

0
source

All Articles