如何处理iOS4.0 +的iPhone模拟器NSInvocation问题

时间:2010-10-13 10:26:48

标签: objective-c ios4 ios-simulator nsinvocation

对于iPhone模拟器iOS4.0 +,NSInvocation不能很好地处理异常。我遇到workaround来使用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;
}

1 个答案:

答案 0 :(得分:0)

我通过完全删除NSInvocation并在选择器上调用objc_msgSend找到了解决方案。此解决方法有助于解决所有NSInvocation异常处理。