2014-12-02 54 views
0

我試圖在iOS 8中運行此代碼,但我在調用方法中遇到錯誤的訪問錯誤,這在iOS 7中運行正常。有沒有人有關於此的線索?NSInvocation調用在iOS中提供錯誤的訪問8

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

}

感謝您的意見。

回答

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


附: [[self class] instanceMethodSignatureForSelector:selector]可以寫成[self methodSignatureForSelector:selector]


p.p.s.如果方法簽名在編譯時已知且已修復,如果您很勇敢,則可以直接使用objc_msgSend

+0

感謝您的評論,我現在就試試。我無法使用objc_msgSend我得到一個錯誤,告訴我它不能用於C99編譯器。 – 2014-12-02 21:25:32

+0

剛剛經過測試,它的工作:D。隊友的歡呼聲!! – 2014-12-02 21:46:05