gpt4 book ai didi

ios - 如何在 iOS 上调配类方法?

转载 作者:IT王子 更新时间:2023-10-29 07:47:26 25 4
gpt4 key购买 nike

Method swizzling 非常适合实例方法。现在,我需要调配一个类方法。知道怎么做吗?

试过这个但它不起作用:

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);
}

最佳答案

事实证明,我并不遥远。这个实现对我有用:

void SwizzleClassMethod(Class c, SEL orig, SEL new) {

Method origMethod = class_getClassMethod(c, orig);
Method newMethod = class_getClassMethod(c, new);

c = object_getClass((id)c);

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);
}

关于ios - 如何在 iOS 上调配类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3267506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com