方法混合对于示例方法非常有效。现在,我需要混合一个类方法。知道怎么做吗
尝试了这个,但它不工作:
void SwizzleClassMethod(Class c, SEL orig, SEL new) {
Method origMethod = class_getClassMethod(c, orig);
Method newMethod = class_getClassMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
2条答案
按热度按时间xpszyzbs1#
结果,我就在附近。这个实现对我来说很有效:
31moq8wy2#
Objective-C swizzling
Objective-C
使用类别搅拌NSObject+Swizzling.h
NSObject+Swizzling。m
使用Objective-C
Add Swift as an consumer(https://stackoverflow.com/a/59217109/4770877)
通过Swift
Swift swizzling(https://stackoverflow.com/a/62412970/4770877)