小编Nis*_*sha的帖子

NSInvocation setArgument不能使用简单的int32_t

我在使用NSInvocation时使用非对象的参数时遇到问题.我传递的简单整数值变为不同的东西.

这是我调用的方法:

+(NSString*) TestMethod2 :(int32_t) number
{
    NSLog(@"*************** %d ******************", number);
    return @"success";
}
Run Code Online (Sandbox Code Playgroud)

这就是我所说的:

-(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];

}
Run Code Online (Sandbox Code Playgroud)

结果是记录的不是我传递的简单123456值,但是这样的事情:

*** …

integer objective-c nsinvocation

2
推荐指数
1
解决办法
3048
查看次数

标签 统计

integer ×1

nsinvocation ×1

objective-c ×1