I am facing an issue while using NSInvocation with arguments which are not objects. The simple integer value that I pass gets changed to something different.
Here is the method I am invoking:
+(NSString*) TestMethod2 :(int32_t) number
{
NSLog(@"*************** %d ******************", number);
return @"success";
}
And This is how I am calling it :
-(void) TestInvocationUsingReflection
{
id testClass = NSClassFromString(@"TestClass");
NSMethodSignature * methodSignature = [testClass methodSignatureForSelector:@selector(TestMethod2:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSignature];
[inv setSelector:@selector(TestMethod2:)];
[inv setTarget:testClass];
NSNumber * arg = [NSNumber numberWithInt:123456];
[inv setArgument:&arg atIndex:2]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
[inv retainArguments];
NSUInteger length = [[inv methodSignature] methodReturnLength];
id result = (void *)malloc(length);
[inv invoke];
[inv getReturnValue:&result];
}
The result is what gets logged is not the simple 123456 value I pass but something like this:
****** 180774176 *********
What is it that I am doing wrong?
I am pretty new to Objective C but, I need to invoke a method at runtime over which I have no control. And it takes int64_t as the argument type.
Please can anyone help? Thanks....