tags:

views:

22

answers:

1

I have used class_replaceMethod function and it works good with instance methods, but I it doesn't work with class method replacement.

Has anybody idea why and what should I do to replace class method implementation?

+2  A: 

If you have a Class (we'll call it MyClass), then you have to get its meta class to operate on class methods.

In other words:

Class myClass = [MyClass class];
unsigned int numInstanceMethods = 0;
Method * instanceMethods = class_copyMethodList(myClass, &numInstanceMethods);
//instanceMethods is an array of all instance methods for MyClass

Class myMetaClass = objc_getMetaClass(class_getName(myClass));
unsigned int numClassMethods = 0;
Method * classMethods = class_copyMethodList(myMetaClass, &numClassMethods);
//classMethods is an array of all class methods for MyClass

Basically, the class is for operating on instance-level stuff, and the meta class is for operating on class-level stuff.

Hopefully this is enough to help you figure out where to go from here. :)

More awesomely useful information: http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

Dave DeLong
Great! Thank you! Your answer is very useful.
Edward