且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时替换 Objective-C 2.0 类方法实现

更新时间:2022-12-04 10:27:20

如果你有一个 Class(我们称之为 MyClass),那么你必须得到它的元类对类方法进行操作.

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

换句话说:

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. :)

更多有用的信息:http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html