gpt4 book ai didi

iOS: class_addMethod 添加的类方法不能被 NSInvocation 使用

转载 作者:行者123 更新时间:2023-11-29 10:42:36 25 4
gpt4 key购买 nike

我使用运行时特性向类中添加了一个类方法,但是NSInvocation 无法使用该方法。我的代码是这样的:

id metaClass = object_getClass((id)protocolClass);
IMP prevImp = class_replaceMethod(metaClass, @selector(xxx), imp, NULL);

const char *selectorName = sel_getName(@selector(xxx));
char newSelectorName[strlen(selectorName) + 10];
strcpy(newSelectorName, "ORIGIN");
strcat(newSelectorName, selectorName);
SEL newSelector = sel_getUid(newSelectorName);
if(!class_respondsToSelector(metaClass, newSelector)) {
class_addMethod(metaClass, newSelector, prevImp, NULL);
}

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[protocolClass methodSignatureForSelector:newSelector]];

创建语法崩溃的调用如下:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSMethodSignature signatureWithObjCTypes:]: type signature is empty.'
*** First throw call stack:
(
0 CoreFoundation 0x07c251e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x079a48e5 objc_exception_throw + 44
2 CoreFoundation 0x07c13ce4 +[NSMethodSignature signatureWithObjCTypes:] + 1172
3 CoreFoundation 0x07cc22e9 +[NSObject(NSObject) methodSignatureForSelector:] + 73
......
)

有什么解释吗?我需要使用NSInvocation 的原因是因为我想要选择器的返回值,还有其他方法吗?

最佳答案

您需要将方法的签名字符串作为class_addMethod 的第四个参数传递。您正在传递 NULL

关于iOS: class_addMethod 添加的类方法不能被 NSInvocation 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23807164/

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