iOS开发:Runtime常见方法

Posted wuwuFQ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发:Runtime常见方法相关的知识,希望对你有一定的参考价值。

iOS开发:认识一下Runtime

iOS开发:Runtime常见方法

iOS开发:Runtime解决UITapGesture重复点击问题

iOS开发:Runtime解决UIButton重复点击问题

获取属性列表

objc_property_t *propertyList = class_copyPropertyList([self class], &count) 
    for (unsigned int i = 0; i < count; i++) 
        const char *propertyName = property_getName(propertyList[i]);
        NSLog(@"property ----->%@",[NSString stringWithUTF8String:propertyName]);
    

获取方法列表

Method *methodList = class_copyMethodList([slef class], &count) ;
for (unsigned int i = 0; i < count; i++ ) 
    Method method = methodList[i];
    NSLog(@"method ----->%@",NSStringFromSelector(method_getName(method)));

获取成员变量列表

Ivar *ivarList = class_copyIvarList([self class], &count);
for (unsigned int i = 0; i < count; i++ ) 
    Ivar myIvar = ivarList[i];
    const char *ivarName = ivar_getName(myIvar);
    NSLog(@"Ivar -----> %@",[NSString stringWithUFT8String:ivarName]);

获取协议列表

__unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count);
for (unsigned int i; i<count; i++)  
    Protocol *myProtocol = protocolList[i];
    const char *protocolName = protocol_getName(myProtocol);
    NSLog(@"protocol -----> %@", [NSString stringWithUTF8String:protocolName]);

现在有一个Person类,和Person类创建的xiaoming对象,和test1 和test2方法。

获得类方法

Class PersonClass = object_getClass([Person class]);

SEL oriSEL = @selectot(test1);
Method oriMethod = class_getInstanceMethod([xiaoming Class], oriSEL);

获得实例方法

Class PersonClass = object_getClass([xiaoming class]);
SEL oriSEL = @selectot(test2);
Method cusMethod = class_getInstanceMethod([xiaoming class], oriSEL);

添加方法

BOOL addsucc = class_addMethod(xiaomingClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));

替换原方法实现

class_replaceMethod(toolClaa, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMetod));

交换方法

method_exchangeImplementations(oriMethod, cusMethod);

以上是关于iOS开发:Runtime常见方法的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发:Runtime常见方法

iOS开发:认识一下Runtime

iOS开发:认识一下Runtime

iOS开发:Runtime解决UITapGesture重复点击问题

iOS开发:Runtime解决UIButton重复点击问题

iOS开发:Runtime解决UIButton重复点击问题