Leonardo Marques
Leonardo Marques

Reputation: 3951

Objective-C swizzled method not receiving arguments

I'm developing an app that needs to perform some swizzling. I'm swizzling a method -(void)m1:(CMAcceleration)a; with another one that I provide.

-(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);
}

To swizzle it I use:

origImp=method_setImplementation(method, newImp);

I then call it normally like [ClassInstance m1:a]; The thing is, args seems to be filled with garbage when I expected a structure like {name=type...} as described in here. I need to pass the arguments to the original implementation after doing some operation like NSLog.

Searching the Internet it seems this is a Simulator problem related but I'm not sure and I have no access to a device to confirm this.

Am I doing something wrong or is there a way to fix this?

Upvotes: 0

Views: 253

Answers (1)

Bryan Chen
Bryan Chen

Reputation: 46598

You are doing it very wrong.

The method signature should match i.e. -(void)newM:(CMAcceleration)a;

and

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

A different way is make C function

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

}

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

Upvotes: 1

Related Questions