NSInvocation调用在iOS 8中提供错误访问

时间:2014-12-02 17:05:23

标签: reflection ios8 invoke nsinvocation

我试图在iOS 8中运行此代码但是我在调​​用的方法中遇到了错误的访问错误,这在iOS 7中运行正常。有没有人对此有所了解?

-(double) calcularColumna:(int ) anio :(int) mes :(NSString * ) columna {
NSInvocation * invocation = [selectores objectForKey:columna];
if(!invocation){
    NSString * metodo = [NSString stringWithFormat:@"fondo%@:anio:mes:",columna];
    SEL selector = NSSelectorFromString(metodo);
    if( ![self respondsToSelector:selector]) {
        return -1;
    }
    invocation = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:selector]];
    invocation.selector = selector;
    invocation.target = self;

    [invocation setArgument:(__bridge void *)(self.valoresEntrada) atIndex:2];
    [selectores setObject:invocation forKey:columna];
}
double valor = 0;
[invocation setArgument:&anio atIndex:3];
[invocation setArgument:&mes atIndex:4];
[invocation invoke];
[invocation getReturnValue:&valor];
/* }else {
 valor = -1;
 }*/
return valor;

}

感谢您的评论。

1 个答案:

答案 0 :(得分:0)

[invocation setArgument:(__bridge void *)(self.valoresEntrada) atIndex:2];

错了。您需要将指针传递给传递的值。像

这样的东西
// I don't know the type of self.valoresEntrada, but you can use the type directly
typeof(self.valoresEntrada) temp = self.valoresEntrada;
[invocation setArgument:&temp atIndex:2];

此外,如果您要将调用存储在集合中以供在其创建的范围之后使用,则需要执行[invocation retainArguments];


P.S。 [[self class] instanceMethodSignatureForSelector:selector]可以写成[self methodSignatureForSelector:selector]


p.p.s。如果方法签名是已知的并在编译时修复,那么如果你是勇敢的话,可以直接使用objc_msgSend