开发者

Monkey Patch an Instance in Objective-C

I'm trying to follow the (brilliant) instructions from here, but I have an instance of a cl开发者_高级运维ass that I cannot modify by subclassing. Is there any way to override methods to an instance of the class only?


Beware. You are about to enter some fairly deep magic in ObjC, and the kinds of bugs you can generate are mind-bending. These are the last techniques you should use after everything else has failed, and you should do whatever you can to isolate this code. That said, ObjC is a highly dynamic language, and you can rewire most things.

First, to your specific question, you can change the class of an instance, which will achieve what you want. The rules are basically:

  • The new class should be a subclass of the former class
  • The new class must not add any ivars. Adding ivars in the subclass is one of the things that can lead to the above-mentioned mind-bending bugs.

You will get no warnings or errors if you do this wrong. You will just get bizarre crashes.

Given that, changing an object's class is actually very easy:

object->isa = [NewClass class];

Beyond that, you can change the implementation of a method for every instance of a class using method swizzling. I have some examples of this in Hijacking with method_exchangeImplementations(). Good luck using the debugger once you've done this, though.


Actually, you can modify anything with subclassing. It is one of the more delicate things you can do, and you can look into method swizling instead (but method swizling will effect all members of the class), but in objective c, it's super easy to set the class of any object, so basically all you need to do is declare a subclass which doesn't declare any extra ivars, and only overrides the methods you need, (and you probably want to call the super implementation), and then change the class of an object at runtime through object_setClass (declared in ).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜