Objective-C swizzled方法没有接收参数

时间:2015-12-13 21:08:04

标签: objective-c swizzling

我正在开发一款需要执行一些调配的应用。 我正在调用方法-(void)m1:(CMAcceleration)a;和我提供的另一个方法。

-(void)newM(id self, SEL _cmd, ...){
va_list args;
va_start(args, _cmd);
//...
NSInteger returnValue=((NSInteger(*)(id,SEL,...))origImp)(self,_cmd,args);
va_end(args);
}

为了调配我使用:

origImp=method_setImplementation(method, newImp);

我接着称之为[ClassInstance m1:a]; 问题是,当我按照here中的描述预期{name=type...}这样的结构时,args似乎充满了垃圾。 我需要在执行一些操作(如NSLog)之后将参数传递给原始实现。

在互联网上搜索这似乎与模拟器有关,但我不确定,我无法访问设备来确认这一点。

我做错了什么或有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

你做错了。

方法签名应与-(void)newM:(CMAcceleration)a;

匹配

Method method = class_getInstanceMethod([SomeClass class],@selector(newM:));
IMP newImp = method_getImplementation(method);
origImp=method_setImplementation(method, newImp);

另一种方法是使C功能

void newM(id self, SEL _cmd, CMAcceleration a) {

}

origImp=method_setImplementation(method, (IMP)newM);