【问题标题】:How to handle iPhone Simulator NSInvocation issue for iOS4.0+如何处理 iOS4.0+ 的 iPhone 模拟器 NSInvocation 问题
【发布时间】:2011-04-24 18:35:42
【问题描述】:

对于 iPhone Simulator 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;
}

【问题讨论】:

    标签: objective-c ios4 ios-simulator nsinvocation


    【解决方案1】:

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

    【讨论】:

    • 你是如何处理方法参数的?我有同样的问题,但参数的数量只在运行时知道。 NSInvocation 使这很容易,但 objc_msgSend 没有。
    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多