Reflection NSInvocation调用在iOS 8中提供错误访问
我试图在iOS 8中运行此代码,但在调用的方法中遇到了错误的访问错误,在iOS 7中运行正常。有人对此有线索吗Reflection NSInvocation调用在iOS 8中提供错误访问,reflection,ios8,invoke,nsinvocation,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 string
-(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;
}
谢谢你的评论
[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、 另外,如果方法签名在编译时是已知的并且是固定的,如果您勇敢的话,可以直接使用objc_msgSend。谢谢您的评论,我现在就试试。我不能使用objc_msgSend,我收到一个错误,告诉我它不能与C99编译器一起使用。