gpt4 book ai didi

iphone - 如何使用 NSInitation 调用类方法?

转载 作者:行者123 更新时间:2023-12-03 19:43:41 24 4
gpt4 key购买 nike

我有一个类方法,未在 h 文件中声明,但在 m 文件中实现。现在我想在另一个类中调用它,由于返回值是一个int,所以我不能直接使用selector,所以我使用NSInitation。

下面是我正在做的事情:

SEL selector = ***;
NSMethodSignature *signature = [classA methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
invocation.target = [classA class];
[invocation setArgument:(void *)arg atIndex:2];
[invocation invoke];

调用没有成功,为什么?

最佳答案

传递参数时,您传递的是它们的地址,而不是它们的值。请尝试以下操作:

[invocation setArgument:&arg
atIndex:2];

参见[NSInvocation setArgument:atIndex:]在类引用中。

关于iphone - 如何使用 NSInitation 调用类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16542532/

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