对于 iPhone Simulator iOS4.0+,NSInvocation 不能很好地处理异常。我遇到了使用 objc_msgSend的解决方法。当我尝试将其作为 objc_msgSend(target_, [invocation selector]) 进行以下调用并在 createResponseFromInvocation() 下注释掉 [invocation invoke] 时会挂起。我尝试了不同的调用 obj_msgSend 的方法,但没有奏效。
- (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;
}