使用带有三个或更多参数的performSelector?

发布于 2024-12-08 13:20:06 字数 82 浏览 3 评论 0原文

各种performSelector:... 方法最多可以处理传递给指定选择器的两个参数。如果我需要传递三个或更多参数怎么办?

The various performSelector:... methods can handle a maximum of two arguments passed to the specified selector. What can I do if I need to pass three or more arguments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-12-15 13:20:06

您需要为此使用 NSInitation 类。请查看这个问题,了解有关使用它们的更多详细信息。

You need to use NSInvocation class for that. Check this SO question for more details on using them.

笑,眼淚并存 2024-12-15 13:20:06

我不喜欢 NSInitation 方式,它需要太多代码。

如果您想立即执行选择器,这里有一个简单而干净的方法:

// Assume we have these variables
id target, SEL aSelector, id parameter1, id parameter2;

// Get the method IMP, method is a function pointer here.
id (*method)(id, SEL, id, id) = (void *)[vc methodForSelector:aSelector];

// IMP is just a C function, so we can call it directly.
id returnValue = method(vc, aSelector, parameter1, parameter2);

I dislike the NSInvocation way, it needs too much code.

If you’d like perform the selector immediately, here is an simple and clean way:

// Assume we have these variables
id target, SEL aSelector, id parameter1, id parameter2;

// Get the method IMP, method is a function pointer here.
id (*method)(id, SEL, id, id) = (void *)[vc methodForSelector:aSelector];

// IMP is just a C function, so we can call it directly.
id returnValue = method(vc, aSelector, parameter1, parameter2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文