Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Method Swizzing中一般替换方法都写在Category类别里吗?有没有别的实现方式


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
330 views
Welcome To Ask or Share your Answers For Others

1 Answer

不一定非写分类,你调下面的方法进行方法交换,位置不随便写吗?

void methodSwizzle(Class c, SEL orig, SEL newS )
{
    Method origMethod = class_getInstanceMethod(c, orig);
    Method newMethod = class_getInstanceMethod(c, newS);
    
    BOOL addSuccess = class_addMethod(c, orig, method_getImplementation(newMethod),method_getTypeEncoding(newMethod) );
    
    if (addSuccess) {
        class_replaceMethod(c, newS, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
        
    }else{
        method_exchangeImplementations(origMethod, newMethod);
        
    }
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...