2010-10-13 163 views
0

對於iPhone模擬器iOS4.0 +,NSInvocation不能很好地處理異常。我碰到一個workaround使用objc_msgSend。當我嘗試將下面的調用作爲objc_msgSend(target_,[調用選擇器])並在createResponseFromInvocation()下注釋掉[調用調用]時掛起。我嘗試了不同的方式調用obj_msgSend,並沒有奏效。如何處理iPhone模擬器NSInvocation問題iOS4.0 +


- (NSInvocation *)createInvocationWithSelector:(SEL)selector 
            signature:(NSMethodSignature *)method 
            arguments:(NSDictionary *)arguments { 
    NSInvocation *invocation 
    = [NSInvocation invocationWithMethodSignature:method]; 
    [invocation setSelector:selector]; 
    [invocation setTarget:target_]; 

    if (arguments != nil) { 
    if ([method numberOfArguments] > 2) { 
     [invocation setArgument:&arguments atIndex:2]; 
    } 
    } 

    return invocation; 
} 

// Invoke the given invocation and create a response from it. 
- (WDResponse *)createResponseFromInvocation:(NSInvocation *)invocation { 
    WDResponse *response; 

    @try { 
    [invocation invoke]; 
    if ([[invocation methodSignature] methodReturnLength] == 0) { 
     response = [WDResponse responseWithValue:nil]; 
    } else { 
     id result; 
     [invocation getReturnValue:&result]; 
     response = [WDResponse responseWithValue:result]; 
    } 
    } 
    @catch (NSException * e) { 
    NSLog(@"Method invocation error: %@", e); 
    response = [WDResponse responseWithError:e]; 
    } 
    return response; 
} 

回答

0

我發現一個解決方案,通過完全刪除NSInvocation並在選擇器上調用objc_msgSend。此解決方法有助於解決所有NSInvocation異常處理。

+0

你是如何處理方法參數的?我有同樣的問題,但參數的數量只有在運行時才知道。 NSInvocation使這很容易,但objc_msgSend不會。 – drekka 2011-10-01 07:07:56