简体   繁体   English

如何从被调用的方法实现创建NSInvocation?

[英]How to create an NSInvocation from a called method implementation?

I have a function that looks like this: 我有一个看起来像这样的函数:

void myMethodImpl(id self, SEL _cmd, ...)

I use this as a implementation for a method on a class 我使用它作为类的方法的实现

class_addMethod(aClass, aSelector, (IMP)myMethodImpl, types);

so myMethodImpl get's called when a message is sent to aClass with selector aSelector. 所以当使用选择器aSelector将消息发送到aClass时,会调用myMethodImpl Now there I'd like to create an NSInvocation with all parameters from myMethodImpl . 现在,我想创建一个包含myMethodImpl所有参数的NSInvocation。

Is there an easy way to create a NSInvocation from the parameter list or do I have to check every element for it's type and add it accordingly? 有没有一种从参数列表创建NSInvocation的简单方法,还是我必须检查每个元素的类型并相应地添加它?

Since you're adding a method to the class, why not add/swizzle forwardInvocation: instead? 既然你要在类中添加一个方法,为什么不添加/ swizzle forwardInvocation:相反? At that point the runtime will have nicely built an invocation for you. 此时,运行时将很好地为您构建调用。

This is not possible. 这是不可能的。 A simple google search shows this article: 一个简单的谷歌搜索显示这篇文章:

http://www.wincent.com/a/about/wincent/weblog/archives/2006/03/nsinvocation_an.php http://www.wincent.com/a/about/wincent/weblog/archives/2006/03/nsinvocation_an.php

Which clearly explains how va_args don't properly work with NSInvocation. 这清楚地解释了va_args如何不能正确使用NSInvocation。

It is possible, indeed. 确实有可能。 Have a look to the actionSelectorImplementation function in https://github.com/rlopezdiez/TMInstanceMethodSwizzler/blob/master/Classes/TMInstanceMethodSwizzler.m 请参阅https://github.com/rlopezdiez/TMInstanceMethodSwizzler/blob/master/Classes/TMInstanceMethodSwizzler.m中的actionSelectorImplementation函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM