5

In Objective-C, you can change an object's dynamic type at runtime by assigning to it's isa member variable:

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

Is this undefined behavior? I'm currently doing this as a kludge for something else, and it appears to be working, but I feel so dirty doing it this way. The new class I'm setting doesn't add any member variables, it just overrides one method and adds a new one, so the class size is the same. I feel like if I changed the object size, much badness would result.

2

4 Answers 4

1

I think this is, as you say, dirty.

I suspect it will work if:

  • The newly assigned class is a subclass of the real class.
  • The new class doesn't add any member variables.

But I'll admit I don't know the nuts-and-bolts implementation of your Objective-C runtime system. I just know what makes sense to me for an implementation.

Sign up to request clarification or add additional context in comments.

Comments

1

The Objective-C runtime now provides a function to do this: object_setClass. The documentation doesn't say what restrictions there are, but (as others have stated) I imagine you would want to restrict the new class to a class with exactly the instance variable layout as the original class.

Comments

0

It's really bad form to change these, and it makes all sorts of assumptions about runtime behaviour -- you're much better off using the runtime functions although they don't provide you with a mechanism to directly change the class.

Comments

0

This answer by Joshua Weinberg provides the right way to do this. It worked for me. :)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.