如何从被调用的方法实现创建 NSInitation?
我有一个如下所示的函数:
void myMethodImpl(id self, SEL _cmd, ...)
我使用它作为类上方法的实现,
class_addMethod(aClass, aSelector, (IMP)myMethodImpl, types);
因此当使用选择器 aSelector 将消息发送到 aClass 时,将调用 myMethodImpl
。现在我想使用 myMethodImpl
中的所有参数创建一个 NSInitation。
有没有一种简单的方法可以从参数列表创建 NSInitation,或者我是否必须检查每个元素的类型并相应地添加它?
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. Now there I'd like to create an NSInvocation with all parameters from myMethodImpl
.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然您要向类添加方法,为什么不添加/调整
forwardInitation:
呢?那时,运行时将很好地为您构建一个调用。Since you're adding a method to the class, why not add/swizzle
forwardInvocation:
instead? At that point the runtime will have nicely built an invocation for you.这是不可能的。简单的谷歌搜索显示这篇文章:
http:// /www.wincent.com/a/about/wincent/weblog/archives/2006/03/nsin Vocation_an.php
这清楚地解释了
va_args
不能正确使用 NSInspiration。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
Which clearly explains how
va_args
don't properly work with NSInvocation.确实有可能。查看 https://github.com/rlopezdiez 中的 actionSelectorImplementation 函数/TMInstanceMethodSwizzler/blob/master/Classes/TMInstanceMethodSwizzler.m
It is possible, indeed. Have a look to the actionSelectorImplementation function in https://github.com/rlopezdiez/TMInstanceMethodSwizzler/blob/master/Classes/TMInstanceMethodSwizzler.m