gpt4 book ai didi

ios - 使用两个以上的参数在 Objective-C 中执行动态类方法调用

转载 作者:可可西里 更新时间:2023-11-01 05:13:12 34 4
gpt4 key购买 nike

我有一个方法可以动态创建不同类的新对象,并希望能够在创建这些类时对其执行选择器。使用 performSelector: withObject: 会奏效,但这些方法有四个参数。我使用 NSInvocation 尝试了以下代码,但收到有关它是无法识别的选择器的错误。

NSInvocation *call = [NSInvocation invocationWithMethodSignature:[NSClassFromString(className) methodSignatureForSelector:@selector(packWithName:value:writer:forClass:)]];
[call setArgument:&arg1 atIndex:0];
[call setArgument:&arg2 atIndex:1];
[call setArgument:&arg3 atIndex:2];
[call setArgument:&arg4 atIndex:3];
call.target = NSClassFromString(className);
[call invoke];

它还会产生以下日志语句:

*** NSForwarding: warning: selector (0x8ed78d0) for message '[garbled random characters]'
does not match selector known to Objective C runtime (0x8b0cd30)-- abort

我还尝试使用 alloc/init 创建 NSInvocation 并像这样设置 @selector:

NSInvocation *call = [[NSInvocation alloc] init];
call.selector = @selector(nameofselector);

然而,这会导致 call 为 nil,所以我猜这是不允许的。

我是否遗漏了有关 NSInvocation 工作原理的信息,或者是否有更智能的方法来做到这一点?

最佳答案

索引 0 和 1 处的参数不是方法调用的前两个显式参数,而是隐式的 self_cmd 参数。请改用索引 2、3、4 和 5。

关于ios - 使用两个以上的参数在 Objective-C 中执行动态类方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17923828/

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